1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#if 0
################################################################################
file=lua2php.cpp
g++ -g -I../../../rts/System $file ../../../dist/unitsync.so
mod=$1
if [ -z $mod ]; then
echo "usage: $0 <ModArchiveName> {ex: ca.sdd, ba631.sd7}"
exit 1
fi
echo ./a.out $mod
./a.out $mod
exit
################################################################################
#endif
/******************************************************************************/
/******************************************************************************/
#include "../unitsync_api.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
/******************************************************************************/
/******************************************************************************/
static void OutputPHPTable(const std::string& indent);
static FILE* outfile = NULL;
#define OUTF(...) fprintf(outfile, __VA_ARGS__)
/******************************************************************************/
/******************************************************************************/
// from VFSModes.h
#define SPRING_VFS_RAW "r"
#define SPRING_VFS_MOD "M"
#define SPRING_VFS_MAP "m"
#define SPRING_VFS_BASE "b"
#define SPRING_VFS_NONE " "
#define SPRING_VFS_MOD_BASE SPRING_VFS_MOD SPRING_VFS_BASE
#define SPRING_VFS_ZIP SPRING_VFS_MOD SPRING_VFS_MAP SPRING_VFS_BASE
/******************************************************************************/
/******************************************************************************/
int main(int argc, char** argv)
{
if (argc < 2) {
printf("usage: %s <mod>\n", argv[0]);
exit(1);
}
const std::string mod = argv[1];
printf("MOD = %s\n", mod.c_str());
// open the output file in the local directory, before
// initializing unitsync (which changes the working directory)
const std::string outName = mod + ".php";
printf("outName = %s\n", outName.c_str());
outfile = fopen(outName.c_str(), "w");
if (outfile == NULL) {
perror("fopen");
return 1;
}
Init(false, 0);
printf("GetSpringVersion() = %s\n", GetSpringVersion());
// load the mod archives
AddAllArchives(mod.c_str());
// print the defs
lpOpenFile("gamedata/defs.lua", SPRING_VFS_MOD_BASE , SPRING_VFS_ZIP);
if (!lpExecute()) {
printf("%s\n", lpErrorLog());
}
else {
lpRootTable();
OUTF("<?php\n");
OUTF("$defs = array(\n");
OutputPHPTable("\t");
OUTF(");\n");
OUTF("?>\n");
}
lpClose();
UnInit();
fclose(outfile);
printf("%s has been created\n", outName.c_str());
return 0;
}
/******************************************************************************/
/******************************************************************************/
static void GetIntKeys(std::vector<int>& keys)
{
const int count = lpGetIntKeyListCount();
for (int i = 0; i < count; i++) {
keys.push_back(lpGetIntKeyListEntry(i));
}
}
static void GetStrKeys(std::vector<std::string>& keys)
{
const int count = lpGetStrKeyListCount();
for (int i = 0; i < count; i++) {
keys.push_back(lpGetStrKeyListEntry(i));
}
}
static inline std::string IntToString(int i, const std::string& format = "%i")
{
char buf[64];
snprintf(buf, sizeof(buf), format.c_str(), i);
return std::string(buf);
}
// from lua.h
#define LUA_TNONE (-1)
#define LUA_TNIL 0
#define LUA_TBOOLEAN 1
#define LUA_TLIGHTUSERDATA 2
#define LUA_TNUMBER 3
#define LUA_TSTRING 4
#define LUA_TTABLE 5
#define LUA_TFUNCTION 6
#define LUA_TUSERDATA 7
#define LUA_TTHREAD 8
static std::string SafeStr(const std::string& str, char escChar = '\'')
{
std::string newStr;
for (unsigned int i = 0; i < str.size(); i++) {
const char c = str[i];
if (c == escChar) {
newStr.push_back('\\');
}
newStr.push_back(c);
}
return newStr;
}
static void OutputPHPTable(const std::string& indent)
{
std::vector<int> intKeys; GetIntKeys(intKeys);
std::vector<std::string> strKeys; GetStrKeys(strKeys);
const char* ind = indent.c_str();
for (int i = 0; i < intKeys.size(); i++) {
const int key = intKeys[i];
const int type = lpGetIntKeyType(key);
if (type == LUA_TTABLE) {
lpSubTableInt(key);
OUTF("%s%i => array(\n", ind, key);
OutputPHPTable(indent + "\t");
OUTF("%s),\n", ind);
lpPopTable();
} else {
if (type == LUA_TNUMBER) {
OUTF("%s%i => %g,\n", ind, key, lpGetIntKeyFloatVal(key, 0.0f));
} else if (type == LUA_TBOOLEAN) {
OUTF("%s%i => %s,\n", ind, key, lpGetIntKeyBoolVal(key, 0) ? "true" : "false");
} else if (type == LUA_TSTRING) {
OUTF("%s%i => '%s',\n", ind, key,
SafeStr(lpGetIntKeyStrVal(key, "BOGUS")).c_str());
} else {
OUTF("//%s%i => bad type (%i)\n", ind, key, type);
}
}
}
for (int i = 0; i < strKeys.size(); i++) {
const char* key = strKeys[i].c_str();
const int type = lpGetStrKeyType(key);
if (type == LUA_TTABLE) {
lpSubTableStr(key);
OUTF("%s\"%s\" => array(\n", ind, key);
OutputPHPTable(indent + "\t");
OUTF("%s),\n", ind);
lpPopTable();
} else {
if (type == LUA_TNUMBER) {
OUTF("%s\"%s\" => %g,\n", ind, key, lpGetStrKeyFloatVal(key, 0.0f));
} else if (type == LUA_TBOOLEAN) {
OUTF("%s\"%s\" => %s,\n", ind, key, lpGetStrKeyBoolVal(key, 0) ? "true" : "false");
} else if (type == LUA_TSTRING) {
OUTF("%s\"%s\" => '%s',\n", ind, key,
SafeStr(lpGetStrKeyStrVal(key, "BOGUS")).c_str());
} else {
OUTF("//%s\"%s\" => bad type (%i)\n", ind, key, type);
}
}
}
}
/******************************************************************************/
/******************************************************************************/
|