loading quests fixed & qc added to build

This commit is contained in:
d1str4ught
2025-08-29 00:15:54 +02:00
parent 916c4c0a6d
commit 526cc3ba56
8 changed files with 1191 additions and 63 deletions

View File

@@ -6,6 +6,7 @@ add_subdirectory(libthecore)
add_subdirectory(liblua)
add_subdirectory(game)
add_subdirectory(db)
add_subdirectory(qc)
if (WIN32)
set_target_properties(common PROPERTIES FOLDER lib)

View File

@@ -19,6 +19,8 @@
#include "guild_manager.h"
#include "sectree_manager.h"
#include <filesystem>
#undef sys_err
#ifndef OS_WINDOWS
#define sys_err(fmt, args...) quest::CQuestManager::instance().QuestError(__FUNCTION__, __LINE__, fmt, ##args)
@@ -594,31 +596,27 @@ namespace quest
for (itertype(g_setQuestObjectDir) it = g_setQuestObjectDir.begin(); it != g_setQuestObjectDir.end(); ++it)
{
const string& stQuestObjectDir = *it;
char buf[PATH_MAX];
snprintf(buf, sizeof(buf), "%s/state/", stQuestObjectDir.c_str());
DIR * pdir = opendir(buf);
std::filesystem::path stateDir = *it;
stateDir /= "state";
int iQuestIdx = 0;
if (pdir)
if (std::filesystem::exists(stateDir) && std::filesystem::is_directory(stateDir))
{
dirent * pde;
while ((pde = readdir(pdir)))
for (const auto& entry : std::filesystem::directory_iterator(stateDir))
{
if (pde->d_name[0] == '.')
continue;
if (entry.is_regular_file())
{
const std::string& filename = entry.path().filename().string();
if (filename.front() == '.')
continue;
snprintf(buf + 11, sizeof(buf) - 11, "%s", pde->d_name);
RegisterQuest(filename, ++iQuestIdx);
int ret = lua_dofile(L, entry.path().string().c_str());
sys_log(0, "QUEST: loading %s, returns %d", entry.path().string().c_str(), ret);
RegisterQuest(pde->d_name, ++iQuestIdx);
int ret = lua_dofile(L, (stQuestObjectDir + "/state/" + pde->d_name).c_str());
sys_log(0, "QUEST: loading %s, returns %d", (stQuestObjectDir + "/state/" + pde->d_name).c_str(), ret);
BuildStateIndexToName(pde->d_name);
BuildStateIndexToName(filename.c_str());
}
}
closedir(pdir);
}
}

View File

@@ -15,9 +15,10 @@
#include "target.h"
#include "party.h"
#include "locale_service.h"
#include "dungeon.h"
#include <filesystem>
DWORD g_GoldDropTimeLimitValue = 0;
extern bool DropEvent_CharStone_SetValue(const std::string& name, int value);
extern bool DropEvent_RefineBox_SetValue (const std::string& name, int value);
@@ -1616,22 +1617,15 @@ namespace quest
m_registeredNPCVnum.insert(dwVnum);
char buf[256];
DIR* dir;
for (itertype(g_setQuestObjectDir) it = g_setQuestObjectDir.begin(); it != g_setQuestObjectDir.end(); ++it)
for (auto it = g_setQuestObjectDir.begin(); it != g_setQuestObjectDir.end(); ++it)
{
const string& stQuestObjectDir = *it;
snprintf(buf, sizeof(buf), "%s/%u", stQuestObjectDir.c_str(), dwVnum);
sys_log(0, "%s", buf);
std::filesystem::path dirPath = std::filesystem::path(*it) / std::to_string(dwVnum);
sys_log(0, "%s", dirPath.string().c_str());
if ((dir = opendir(buf)))
if (std::filesystem::exists(dirPath) && std::filesystem::is_directory(dirPath))
{
closedir(dir);
snprintf(buf, sizeof(buf), "%u", dwVnum);
sys_log(0, "%s", buf);
m_mapNPC[dwVnum].Set(dwVnum, buf);
sys_log(0, "%s", std::to_string(dwVnum).c_str());
m_mapNPC[dwVnum].Set(dwVnum, std::to_string(dwVnum));
}
}
}

View File

@@ -6,6 +6,8 @@
#include "config.h"
#include "char.h"
#include <filesystem>
// questpc.h: PC::typedef Quest
// questpc.h: PC::typedef map<unsigned long, QuestState> QuestInfo;
// typedef
@@ -26,46 +28,30 @@ namespace quest
{
m_vnum = vnum;
char buf[PATH_MAX];
auto& eventNameMap = CQuestManager::instance().m_mapEventName;
CQuestManager::TEventNameMap::iterator itEventName = CQuestManager::instance().m_mapEventName.begin();
while (itEventName != CQuestManager::instance().m_mapEventName.end())
for (const auto& eventPair : eventNameMap)
{
__typeof(itEventName) it = itEventName;
++itEventName;
const std::string& eventName = eventPair.first;
int event_index = eventPair.second;
for (itertype(g_setQuestObjectDir) itObjectDir = g_setQuestObjectDir.begin(); itObjectDir != g_setQuestObjectDir.end(); ++itObjectDir)
{
int is = snprintf(buf, sizeof(buf), "%s/%s/%s/", itObjectDir->c_str(), script_name.c_str(), it->first.c_str());
for (auto itObjectDir = g_setQuestObjectDir.begin(); itObjectDir != g_setQuestObjectDir.end(); ++itObjectDir)
{
std::filesystem::path dirPath = std::filesystem::path(*itObjectDir) / script_name / eventName;
if (is < 0 || is >= (int) sizeof(buf))
is = sizeof(buf) - 1;
//sys_log(0, "XXX %s", buf);
int event_index = it->second;
DIR * pdir = opendir(buf);
if (!pdir)
if (!std::filesystem::exists(dirPath) || !std::filesystem::is_directory(dirPath))
continue;
dirent * pde;
while ((pde = readdir(pdir)))
for (const auto& entry : std::filesystem::directory_iterator(dirPath))
{
if (pde->d_name[0] == '.')
const std::string& filename = entry.path().filename().string();
if (filename.front() == '.' || filename.compare(0, 3, "CVS") == 0)
continue;
if (!strncasecmp(pde->d_name, "CVS", 3))
continue;
sys_log(1, "QUEST reading %s", pde->d_name);
strlcpy(buf + is, pde->d_name, sizeof(buf) - is);
LoadStateScript(event_index, buf, pde->d_name);
sys_log(1, "QUEST reading %s", filename.c_str());
LoadStateScript(event_index, entry.path().string().c_str(), filename.c_str());
}
closedir(pdir);
}
}
}

13
src/qc/CMakeLists.txt Normal file
View File

@@ -0,0 +1,13 @@
file(GLOB_RECURSE QC_SOURCES "*.h" "*.cpp")
add_executable(qc ${QC_SOURCES})
target_link_libraries(qc
liblua
)
if (WIN32)
target_link_libraries(db ws2_32)
else()
target_link_libraries(db pthread md)
endif()

135
src/qc/crc32.cpp Normal file
View File

@@ -0,0 +1,135 @@
#include "crc32.h"
static unsigned long CRCTable[256] =
{
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
};
#define DO1(buf, i) crc = CRCTable[(crc ^ buf[i]) & 0xff] ^ (crc >> 8)
#define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1);
#define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2);
#define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4);
#define DO16(buf, i) DO8(buf, i); DO8(buf, i + 8);
crc_t get_crc32(const char * buf, size_t len)
{
crc_t crc = 0xffffffff;
if (16 <= len)
{
do
{
DO16(buf, 0);
buf += 16;
len -= 16;
} while (len >= 16);
}
if (0 != len)
{
do
{
DO1(buf, 0);
++buf;
--len;
} while (len > 0);
}
crc ^= 0xffffffff;
return crc;
}
/*
#define DO1CI(buf, i) crc = CRCTable[(crc ^ UPPER(buf[i])) & 0xff] ^ (crc >> 8)
#define DO2CI(buf, i) DO1CI(buf, i); DO1CI(buf, i + 1);
#define DO4CI(buf, i) DO2CI(buf, i); DO2CI(buf, i + 2);
#define DO8CI(buf, i) DO4CI(buf, i); DO4CI(buf, i + 4);
#define DO16CI(buf, i) DO8CI(buf, i); DO8CI(buf, i + 8);
crc_t get_crc32_case(const char * buf, size_t len)
{
crc_t crc = 0xffffffff;
if (16 <= len)
{
do
{
DO16CI(buf, 0);
buf += 16;
len -= 16;
} while (len >= 16);
}
if (0 != len)
{
do
{
DO1CI(buf, 0);
++buf;
--len;
} while (len > 0);
}
crc ^= 0xffffffff;
return crc;
}
*/
crc_t get_fast_hash(const char * key, size_t len)
{
const char * end = key + len;
unsigned long h = 0;
while (key < end)
{
h *= 16777619;
h ^= *(key++);
}
return (h);
}

17
src/qc/crc32.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef __INC_CRC32_H__
#define __INC_CRC32_H__
#ifndef OS_WINDOWS
#include <unistd.h>
#endif
typedef unsigned long crc_t;
crc_t get_crc32(const char * buffer, size_t count);
crc_t get_crc32_case(const char * buffer, size_t count);
crc_t get_fast_hash(const char * key, size_t len);
#define CRC32(buf) get_crc32(buf, strlen(buf))
#define CRC32CASE(buf) get_crc32_case(buf, strlen(buf))
#endif

984
src/qc/qc.cpp Normal file
View File

@@ -0,0 +1,984 @@
extern "C"
{
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "liblua/src/lzio.h"
#include "liblua/src/llex.h"
#include "liblua/src/lstring.h"
}
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <fstream>
#include <string>
#include <sstream>
#include <utility>
#include <vector>
#ifndef OS_WINDOWS
#include <unistd.h>
#else
#include <direct.h>
#define mkdir(path, mode) _mkdir(path)
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "crc32.h"
#define OUTPUT_FOLDER "object"
using namespace std;
lua_State* L;
typedef struct LoadF {
FILE *f;
char buff[LUAL_BUFFERSIZE];
} LoadF;
char * g_filename;
unsigned int get_string_crc(const std::string & str)
{
const unsigned char * s = (const unsigned char*) str.c_str();
const unsigned char * end = s + str.size();
unsigned int h = 0;
while (s < end)
{
h *= 16777619;
h ^= (unsigned char) *(unsigned char *) (s++);
}
return h;
}
static const char *getF (lua_State *L, void *ud, size_t *size) {
LoadF *lf = (LoadF *)ud;
if (feof(lf->f))
return NULL;
*size = fread(lf->buff, 1, LUAL_BUFFERSIZE, lf->f);
return (*size > 0) ? lf->buff : NULL;
}
static void next (LexState * ls)
{
ls->lastline = ls->linenumber;
if (ls->lookahead.token != TK_EOS)
{
/* is there a look-ahead token? */
ls->t = ls->lookahead; /* use this one */
ls->lookahead.token = TK_EOS; /* and discharge it */
}
else
ls->t.token = luaX_lex(ls, &ls->t.seminfo); /* read next token */
}
static bool testnext (LexState * ls, int c)
{
if (ls->t.token == c)
{
next(ls);
return true;
}
else
return false;
}
static void lookahead (LexState * ls)
{
lua_assert(ls->lookahead.token == TK_EOS);
ls->lookahead.token = luaX_lex(ls, &ls->lookahead.seminfo);
}
enum parse_state
{
ST_START,
ST_QUEST,
ST_QUEST_WITH_OR_BEGIN,
ST_STATELIST,
ST_STATE_NAME,
ST_STATE_BEGIN,
ST_WHENLIST_OR_FUNCTION,
ST_WHEN_NAME,
ST_WHEN_WITH_OR_BEGIN,
ST_WHEN_BODY,
ST_FUNCTION_NAME,
ST_FUNCTION_ARG,
ST_FUNCTION_BODY,
};
//#define t2obj(tok) ((tok.token == TK_NAME || tok.token == TK_STRING)?getstr(tok.seminfo.ts):((tok.token==TK_NUMBER)?lexstate.t.seminfo.r:luaX_token2str(&lexstate,tok.token)))
struct LexState * pls = 0;
void errorline(int line , const char * str)
{
cout.flush();
if (g_filename)
cerr << g_filename <<":";
cerr << line<< ':';
cerr << str << endl;
abort();
}
void error(const char * str)
{
cout.flush();
if (g_filename)
cout << g_filename <<":";
if (pls)
{
cout << pls->linenumber << ':';
}
cerr << str << endl;
abort();
}
#define assert(exp) if (!(exp)) error("assertion failure : " #exp)
#define assert_msg(exp,msg) if (!(exp)) error(msg " : " #exp)
ostream & operator << (ostream & ostr, const Token& tok)
{
if (tok.token == TK_NAME)
ostr << getstr(tok.seminfo.ts);
else if (tok.token == TK_NUMBER)
ostr << tok.seminfo.r;
else if (tok.token == TK_STRING)
ostr << '"' << getstr(tok.seminfo.ts) <<'"';
else
ostr << luaX_token2str(pls,tok.token);
return ostr;
}
bool check_syntax(const string& str, const string& module)
{
int ret = luaL_loadbuffer(L, str.c_str(), str.size(), module.c_str());
if (ret)
{
cerr << str << endl;
error((string("syntax error : ")+lua_tostring(L,-1) ).c_str());
return false;
}
lua_pop(L,1);
return true;
}
int none_c_function(lua_State* L)
{
return 0;
}
set<string> function_defs;
set<string> function_calls;
void RegisterDefFunction(const string& fname)
{
function_defs.insert(fname);
}
void RegisterUsedFunction(const string& fname)
{
function_calls.insert(fname);
}
void CheckUsedFunction()
{
bool hasError = false;
set<string> error_func;
for (auto it = function_calls.begin(); it != function_calls.end(); ++it)
{
if (function_defs.find(*it) == function_defs.end())
{
hasError = true;
error_func.insert(*it);
}
//cout << "Used : " << *it << endl;
}
if (hasError)
{
cout << "Calls undeclared function! : " << endl;
for (auto it = error_func.begin(); it != error_func.end(); ++it)
{
cout << *it << endl;
}
abort();
}
}
void load_quest_function_list(const char* filename)
{
ifstream inf(filename);
string s;
while (!inf.eof())
{
inf >> s;
if (inf.fail())
break;
RegisterDefFunction(s);
}
}
struct AScript
{
string when_condition;
string when_argument;
string script;
AScript(string a, string b, string c) :
when_condition(a),
when_argument(b),
script(c)
{}
AScript() {}
};
void parse(char * filename)
{
LoadF lf;
lf.f = fopen(filename,"r");
ZIO z;
luaZ_init(&z, getF,&lf,"quest");
Mbuffer b;
struct LexState lexstate;
pls = &lexstate;
luaZ_initbuffer(L,&b);
lexstate.buff = &b;
luaX_setinput(L, &lexstate, &z, luaS_new(L, zname(&z)));
parse_state ps = ST_START;
int nested = 0;
string quest_name;
string start_condition;
string current_state_name;
string current_when_name;
string current_when_condition;
string current_when_argument;
set<string> define_state_name_set;
map<int,string> used_state_name_map;
map<string, map<string, string> > state_script_map;
map<string, map<string, vector<AScript> > > state_arg_script_map;
vector<pair<string, string> > when_name_arg_vector;
string current_function_name;
string current_function_arg;
string all_functions;
load_quest_function_list("quest_functions");
while (1)
{
next(&lexstate);
/*/
cout << luaX_token2str(&lexstate,lexstate.t.token);
if (lexstate.t.token == TK_NAME)
cout << '\t' << getstr(lexstate.t.seminfo.ts);
else if (lexstate.t.token == TK_NUMBER)
cout << '\t' << lexstate.t.seminfo.r;
else if (lexstate.t.token == TK_STRING)
cout << '\t' << '"' << getstr(lexstate.t.seminfo.ts) <<'"';
cout << endl;
//*/
if (lexstate.t.token == TK_EOS) break;
Token& t= lexstate.t;
switch(ps)
{
case ST_START:
{
assert(nested==0);
if (t.token == TK_QUEST)
ps = ST_QUEST;
else
error("must start with 'quest'");
}
break;
case ST_QUEST:
{
assert(nested==0);
if (t.token == TK_NAME || t.token == TK_STRING)
{
quest_name = getstr(lexstate.t.seminfo.ts);
cout << "QUEST : " << quest_name << endl;
ps = ST_QUEST_WITH_OR_BEGIN;
}
else
error("quest name must be given");
}
break;
case ST_QUEST_WITH_OR_BEGIN:
assert(nested==0);
if (t.token == TK_WITH)
{
assert(nested==0);
next(&lexstate);
ostringstream os;
os << (lexstate.t);
//cout << (lexstate.t);
next(&lexstate);
while (lexstate.t.token!=TK_DO)
{
os << " " <<(lexstate.t);
//cout << TK_DO<<lexstate.t.token << " " <<(lexstate.t) <<endl;
next(&lexstate);
}
start_condition = os.str();
check_syntax("if "+start_condition+" then end", quest_name);
cout << "\twith ";
cout << start_condition;
cout << endl;
t = lexstate.t;
}
if (t.token == TK_DO)
{
ps = ST_STATELIST;
nested++;
}
else
{
ostringstream os;
os << "quest doesn't have begin-end clause. (" << t << ")";
error(os.str().c_str());
}
break;
case ST_STATELIST:
{
assert(nested==1);
if (t.token == TK_STATE)
{
ps = ST_STATE_NAME;
}
else if (t.token == TK_END)
{
nested --;
ps = ST_START;
}
else
{
error("expecting 'state'");
}
}
break;
case ST_STATE_NAME:
{
assert(nested==1);
if (t.token == TK_NAME || t.token == TK_STRING)
{
current_state_name = getstr(t.seminfo.ts);
define_state_name_set.insert(current_state_name);
cout << "STATE : " << current_state_name << endl;
ps = ST_STATE_BEGIN;
}
else
{
error("state name must be given");
}
}
break;
case ST_STATE_BEGIN:
{
assert(nested==1);
if (t.token == TK_DO)
{
nested ++;
ps = ST_WHENLIST_OR_FUNCTION;
}
else
{
error("state doesn't have begin-end clause.");
}
}
break;
case ST_WHENLIST_OR_FUNCTION:
{
assert(nested==2);
if (t.token == TK_WHEN)
{
ps = ST_WHEN_NAME;
when_name_arg_vector.clear();
}
else if (t.token == TK_END)
{
nested--;
ps = ST_STATELIST;
}
else if (t.token == TK_FUNCTION)
{
ps = ST_FUNCTION_NAME;
}
else
{
error("expecting 'when' or 'function'");
}
}
break;
case ST_WHEN_NAME:
{
assert(nested==2);
if (t.token == TK_NAME || t.token == TK_STRING || t.token == TK_NUMBER)
{
if (t.token == TK_NUMBER)
{
ostringstream os;
os << (unsigned int)t.seminfo.r;
current_when_name = os.str();
lexstate.lookahead.token = '.';
}
else
{
current_when_name = getstr(t.seminfo.ts);
lookahead(&lexstate);
}
ps = ST_WHEN_WITH_OR_BEGIN;
current_when_argument = "";
if (lexstate.lookahead.token == '.')
{
next(&lexstate);
current_when_name += '.';
next(&lexstate);
ostringstream os;
t = lexstate.t;
os << t;
if (os.str() == "target")
{
current_when_argument = "." + current_when_name;
current_when_argument.resize(current_when_argument.size()-1);
current_when_name = "target";
}
else
{
current_when_name += os.str();
}
lookahead(&lexstate);
}
{
// make when argument
ostringstream os;
while (lexstate.lookahead.token == '.')
{
next(&lexstate);
os << '.';
next(&lexstate);
t = lexstate.t;
//if (t.token == TK_STRING)
//t.token = TK_NAME;
os << t;
lookahead(&lexstate);
}
current_when_argument += os.str();
}
cout << "WHEN : " << current_when_name ;
if (!current_when_argument.empty())
{
cout << " (";
cout << current_when_argument.substr(1);
cout << ")";
}
}
else
{
error("when name must be given");
}
if (lexstate.lookahead.token == TK_OR)
{
// ´ÙÁß when name
// push to somewhere -.-
ps = ST_WHEN_NAME;
when_name_arg_vector.push_back(make_pair(current_when_name, current_when_argument));
next(&lexstate);
cout << " or" << endl;
}
else
{
cout <<endl;
}
}
break;
case ST_WHEN_WITH_OR_BEGIN:
{
assert(nested==2);
current_when_condition = "";
if (t.token == TK_WITH)
{
// here comes Á¶°Ç½Ä
next(&lexstate);
ostringstream os;
os << (lexstate.t);
//cout << (lexstate.t);
next(&lexstate);
while (lexstate.t.token!=TK_DO)
{
os << " " <<(lexstate.t);
//cout << TK_DO<<lexstate.t.token << " " <<(lexstate.t) <<endl;
next(&lexstate);
}
current_when_condition = os.str();
check_syntax("if "+current_when_condition+" then end", current_state_name+current_when_condition);
cout << "\twith ";
cout << current_when_condition;
cout << endl;
t = lexstate.t;
}
if (t.token == TK_DO)
{
ps = ST_WHEN_BODY;
nested++;
}
else
{
//error("when doesn't have begin-end clause.");
ostringstream os;
os << "when doesn't have begin-end clause. (" << t << ")";
error(os.str().c_str());
}
}
break;
case ST_WHEN_BODY:
{
assert(nested==3);
// output
ostringstream os;
int state_check = 0;
auto prev = lexstate;
string callname;
bool registered = false;
if (prev.t.token == '.')
prev.t.token == TK_DO; // any token
while (1)
{
if (lexstate.t.token == TK_DO || lexstate.t.token == TK_IF /*|| lexstate.t.token == TK_FOR*/ || lexstate.t.token == TK_BEGIN || lexstate.t.token == TK_FUNCTION)
{
//cout << ">>>" << endl;
nested++;
}
else if (lexstate.t.token == TK_END)
{
//cout << "<<<" << endl;
nested--;
}
if (!callname.empty())
{
lookahead(&lexstate);
if (lexstate.lookahead.token == '(')
{
RegisterUsedFunction(callname);
registered = true;
}
callname.clear();
}
else if (lexstate.t.token == '(')
{
if (!registered && prev.t.token == TK_NAME)
RegisterUsedFunction(getstr(prev.t.seminfo.ts));
registered = false;
}
if (lexstate.t.token == '.')
{
ostringstream fname;
lookahead(&lexstate);
fname << prev.t << '.' << lexstate.lookahead;
callname = fname.str();
}
if (state_check)
{
state_check--;
if (!state_check)
{
if (lexstate.t.token == TK_NAME || lexstate.t.token == TK_STRING)
{
used_state_name_map[lexstate.linenumber] = (getstr(lexstate.t.seminfo.ts));
lexstate.t.token = TK_STRING;
}
}
}
if (lexstate.t.token == TK_NAME && ((strcmp(getstr(lexstate.t.seminfo.ts),"set_state")==0) || (strcmp(getstr(lexstate.t.seminfo.ts),"newstate")==0) || (strcmp(getstr(lexstate.t.seminfo.ts),"setstate")==0)))
{
state_check = 2;
}
if (nested==2) break;
os << lexstate.t << ' ';
prev = lexstate;
next(&lexstate);
if (lexstate.linenumber != lexstate.lastline)
os << endl;
}
//cout << os.str() << endl;
check_syntax(os.str(), current_state_name+current_when_condition);
reverse(when_name_arg_vector.begin(), when_name_arg_vector.end());
while (1)
{
if (current_when_argument.empty())
{
if (current_when_condition.empty())
state_script_map[current_when_name][current_state_name]+=os.str();
else
state_script_map[current_when_name][current_state_name]+="if "+current_when_condition+" then "+os.str()+" return end ";
}
else
{
state_arg_script_map[current_when_name][current_state_name].push_back(AScript(current_when_condition, current_when_argument, os.str()));
}
if (!when_name_arg_vector.empty())
{
current_when_name = when_name_arg_vector.back().first;
current_when_argument = when_name_arg_vector.back().second;
when_name_arg_vector.pop_back();
}
else
break;
}
ps = ST_WHENLIST_OR_FUNCTION;
}
break;
case ST_FUNCTION_NAME:
if (t.token == TK_NAME)
{
current_function_name = getstr(t.seminfo.ts);
RegisterDefFunction(quest_name+"."+current_function_name);
ps = ST_FUNCTION_ARG;
}
break;
case ST_FUNCTION_ARG:
{
assert(t.token == '(');
next(&lexstate);
current_function_arg = '(';
if (t.token != ')')
{
do
{
if (t.token == TK_NAME)
{
current_function_arg += getstr(t.seminfo.ts);
next(&lexstate);
if (t.token != ')')
current_function_arg += ',';
}
else
{
ostringstream os;
os << "invalud argument name " << getstr(t.seminfo.ts) << " for function " << current_function_name;
error(os.str().c_str());
}
} while (testnext(&lexstate, ','));
}
current_function_arg += ')';
ps = ST_FUNCTION_BODY;
nested ++;
}
break;
case ST_FUNCTION_BODY:
{
assert(nested == 3);
ostringstream os;
auto prev = lexstate;
bool registered = false;
if (prev.t.token == '.')
prev.t.token = TK_DO;
string callname;
while (nested >= 3)
{
if (lexstate.t.token == TK_DO || lexstate.t.token == TK_IF /*|| lexstate.t.token == TK_FOR*/ || lexstate.t.token == TK_BEGIN || lexstate.t.token == TK_FUNCTION)
{
//cout << ">>>" << endl;
nested++;
}
else if (lexstate.t.token == TK_END)
{
//cout << "<<<" << endl;
nested--;
}
if (!callname.empty())
{
lookahead(&lexstate);
if (lexstate.lookahead.token == '(')
{
RegisterUsedFunction(callname);
registered = true;
}
callname.clear();
}
else if (lexstate.t.token == '(')
{
if (!registered && prev.t.token == TK_NAME)
RegisterUsedFunction(getstr(prev.t.seminfo.ts));
registered = false;
}
if (lexstate.t.token == '.')
{
ostringstream fname;
lookahead(&lexstate);
fname << prev.t << '.' << lexstate.lookahead;
callname = fname.str();
}
os << lexstate.t << ' ';
if (nested == 2)
break;
prev = lexstate;
next(&lexstate);
//cout << lexstate.t << ' ' << lexstate.linenumber << ' ' << lexstate.lastline << endl;
if (lexstate.linenumber != lexstate.lastline)
os << endl;
}
ps = ST_WHENLIST_OR_FUNCTION;
all_functions += ',';
all_functions += current_function_name;
all_functions += "= function ";
all_functions += current_function_arg;
all_functions += os.str();
cout << "FUNCTION " << current_function_name << current_function_arg << endl;
}
break;
} // end of switch
}
assert(nested==0);
for (map<int,string>::iterator it = used_state_name_map.begin();it!=used_state_name_map.end();++it)
{
if (define_state_name_set.find(it->second)==define_state_name_set.end())
{
errorline(it->first,("state name not found : "+it->second).c_str());
}
}
if (!define_state_name_set.empty())
{
if (0 != mkdir(OUTPUT_FOLDER "/state", S_IRWXU))
{
if (errno != EEXIST)
{
perror("cannot create directory");
exit(1);
}
}
ofstream ouf((string(OUTPUT_FOLDER "/state/")+quest_name).c_str());
ouf << quest_name << "={[\"start\"]=0";
set<string> :: iterator it;
map<string, int> state_crc;
set<int> crc_set;
state_crc["start"] = 0;
for (it = define_state_name_set.begin(); it != define_state_name_set.end(); ++it)
{
int crc = (int) CRC32((*it).c_str());
if (crc_set.find(crc) == crc_set.end())
{
crc_set.insert(crc);
}
else
{
++crc;
while (crc_set.find(crc) != crc_set.end())
++crc;
printf("WARN: state CRC conflict occur! state index may differ in next compile time.\n");
crc_set.insert(crc);
}
state_crc.insert(make_pair(*it, crc));
}
int idx = 1;
for (it = define_state_name_set.begin(); it!=define_state_name_set.end(); ++it)
{
if (*it != "start")
{
ouf << ",[\"" << *it << "\"]=" <<state_crc[*it];
++idx;
}
}
// quest functionµéÀ» ±â·Ï
ouf << all_functions;
ouf << "}";
}
if (!start_condition.empty())
{
if (0 != mkdir(OUTPUT_FOLDER "/begin_condition", S_IRWXU))
{
if (errno != EEXIST)
{
perror("cannot create directory");
exit(1);
}
}
ofstream ouf((string(OUTPUT_FOLDER "/begin_condition/")+quest_name).c_str());
ouf << "return " <<start_condition;
ouf.close();
}
{
map<string, map<string, vector<AScript> > >::iterator it;
for (it = state_arg_script_map.begin(); it != state_arg_script_map.end(); ++it)
{
string second_name;
string path;
if (it->first.find('.')==it->first.npos)
{
// one like login
string s(it->first);
transform(s.begin(),s.end(),s.begin(),::tolower);
mkdir(OUTPUT_FOLDER "/notarget",0755);
mkdir((OUTPUT_FOLDER "/notarget/"+s).c_str(),0755);
path = OUTPUT_FOLDER "/notarget/"+s+"/";
second_name = s;
}
else
{
// two like [WHO].Kill
string s = it->first;
transform(s.begin(),s.end(),s.begin(),::tolower);
int i = s.find('.');
mkdir((OUTPUT_FOLDER "/"+it->first.substr(0,i)).c_str(),0755);
mkdir((OUTPUT_FOLDER "/"+it->first.substr(0,i)+"/"+s.substr(i+1,s.npos)).c_str(),0755);
path = OUTPUT_FOLDER "/" + it->first.substr(0,i) + "/"+s.substr(i+1,s.npos)+"/";
second_name = s.substr(i+1,s.npos);
}
map<string,vector<AScript> >::iterator it2;
for (it2 = it->second.begin(); it2 != it->second.end(); ++it2)
{
for (int i = 0; i < it2->second.size(); ++i)
{
ostringstream os;
os << i;
{
ofstream ouf((path+quest_name+"."+it2->first+"."+os.str()+"."+"script").c_str());
copy(it2->second[i].script.begin(),it2->second[i].script.end(), ostreambuf_iterator<char>(ouf));
}
{
ofstream ouf((path+quest_name+"."+it2->first+"."+os.str()+"."+"when").c_str());
if (!it2->second[i].when_condition.empty())
{
ouf << "return ";
copy(it2->second[i].when_condition.begin(),it2->second[i].when_condition.end(), ostreambuf_iterator<char>(ouf));
}
}
{
ofstream ouf((path+quest_name+"."+it2->first+"."+os.str()+"."+"arg").c_str());
copy(it2->second[i].when_argument.begin()+1,it2->second[i].when_argument.end(), ostreambuf_iterator<char>(ouf));
}
}
}
}
}
{
map<string,map<string,string> >::iterator it;
for (it = state_script_map.begin();it!=state_script_map.end();++it)
{
string path;
if (it->first.find('.')==it->first.npos)
{
// one
string s = it->first;
transform(s.begin(),s.end(),s.begin(),::tolower);
mkdir(OUTPUT_FOLDER "/notarget",0755);
mkdir((OUTPUT_FOLDER "/notarget/"+s).c_str(),0755);
path = OUTPUT_FOLDER "/notarget/"+s+"/";
}
else
{
// two like [WHO].Kill
string s = it->first;
transform(s.begin(),s.end(),s.begin(),::tolower);
int i = s.find('.');
mkdir((OUTPUT_FOLDER "/"+it->first.substr(0,i)).c_str(),0755);
mkdir((OUTPUT_FOLDER "/"+it->first.substr(0,i)+"/"+s.substr(i+1,s.npos)).c_str(),0755);
path = OUTPUT_FOLDER "/" + it->first.substr(0,i) + "/"+s.substr(i+1,s.npos)+"/";
}
map<string,string>::iterator it2;
for (it2 = it->second.begin();it2!=it->second.end();++it2)
{
ofstream ouf((path+quest_name+"."+it2->first).c_str());
copy(it2->second.begin(),it2->second.end(), ostreambuf_iterator<char>(ouf));
}
}
}
CheckUsedFunction();
}
int main(int argc, char* argv[])
{
mkdir(OUTPUT_FOLDER, 0700);
L = lua_open();
luaX_init(L);
if (argc > 1)
{
for (int i = 1; i < argc; ++i)
{
g_filename = argv[i];
parse(argv[i]);
}
}
lua_close(L);
return 0;
}