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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
//includes
#include "exevm.hpp"
#include "resource.hpp"
#include <fstream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <tclap/CmdLine.h>
#include <luajit-2.0/lua.hpp>
#include <windows.h>
#include <tinyfiledialogs.h>
#define SDL_MAIN_HANDLED
#include <sdl2/sdl.h>
#include "disphelper.h"
#include "AI.hpp"
#include "audio.hpp"
#include "vector.hpp"
#include "joystick.hpp"
#include "timer.hpp"
#include "window.hpp"
#include "console.hpp"
//uses
using namespace std;
using namespace TCLAP;
//variables that we need
lua_State *l;
//compression function for compress the lua scripts
const char* compress (const char* in, int inlen, int* outlen)
{
char* buffer = (char*) malloc(inlen+8);
if (buffer==nullptr)
{
return nullptr;
}
z_stream z;
memset(&z, 0, sizeof(z));
z.next_in = in;
z.avail_in = inlen;
z.next_out = buffer;
z.avail_out = inlen+8;
if (deflateInit(&z,9)!=Z_OK)
{
return nullptr;
}
if (deflate(&z,Z_FINISH)!=Z_STREAM_END)
{
return nullptr;
}
if (outlen)
{
*outlen = inlen +8 -z.avail_out;
}
return buffer;
}
//encryption function in order to prevent the executable from accessed and cracked by others
char* encrypt(char* in, int len)
{
BLOWFISH_CTX c;
Blowfish_Init(&c, encryption_code, len);
char* out;
Blowfish_Encrypt(&c, (int*)in, (int*)out);
return out;
}
//this function writes the executable VM and the script into executable file
int writechunk (lua_State* l, chunkinfo* i, fstream& out, const char* chunkname, bool dbg)
{
int n=0, t=0, length=0, clength=0;
lua_pushboolean(l, !dbg);
lua_call(l, 2, 1);
const char* data = lua_tolstring(l, -1, &length);
const char*cdata = compress(data, length, &clength);
if (cdata==nullptr)
{
return 0;
}
char* encdata=encrypt(cdata, length);
if(encdata==nullptr)
{
return 0;
}
i->offset =out.tellg();
i->length = clength;
while (t<clength && (n=out.write(encdata+t, clength-t))>0)
{
t+=n;
}
free(encdata);
free(cdata);
if (t<clength)
{
return 0;
}
lua_pop(l,1);
return 0;
}
//this function prepares the executable name, in order for it to run
void preparename (char* s)
{
char* c = strrchr(s,'.');
if (c)
{
*c=0;
}
while (*(++s))
{
if (*s=='/' || *s=='\\')
{
*s='.';
}
}
}
//the program main entrypoint
int main(int argc, char** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_JoystickEventState(SDL_ENABLE);
dhInitialize(true);
dhToggleExceptions(true);
l=lua_open();
if(l==nullptr)
{
lua_pushstring(l, "unable to initialize Advanced Audio Game Engine");
lua_error(l);
}
luaL_openlibs(l);
luaL_OpenVector(l);
luaL_OpenAI(l);
luaL_OpenJoystick(l);
luaL_OpenTimer(l);
luaL_OpenWindow(l);
luaL_OpenConsole(l);
char* scriptfile;
char* outfile;
fstream in("exec.vm", ios::in|ios::binary);
bool dbg=false;
bool compile=false;
try
{
CmdLine cmd("Advanced Audio Game Engine", " ", "0.1.0", true);
UnlabeledValueArg<string> scrfile("scriptfile", "script file to load", false, "", "the script file to be loaded", false, nullptr);
cmd.add(scrfile);
ValueArg<string> out("o", "output", "set the executable output file", false, "", "the path to script file to load", nullptr);
cmd.add(out);
SwitchArg compscript("c", "compile", "compile's the script into executable", false, nullptr);
cmd.add(compscript);
SwitchArg debugscript("d", "debug", "add's debugging information to compiled executable", false, nullptr);
cmd.add(debugscript);
cmd.parse(argc, argv);
scriptfile=scrfile.getValue().c_str();
outfile=out.getValue().c_str();
compile=compscript.getValue();
dbg=debugscript.getValue();
}
catch(ArgException &e)
{
cerr << "error: " << e.what();
}
if(argc<2)
{
if(scriptfile="")
{
const char* openfilters[]= {
"*.aage"
};
scriptfile=tinyfd_openFileDialog("select which script to load", "", 1, openfilters, false);
if(scriptfile==nullptr)
{
exit(0);
}
}
if(compile==true&&outfile=="")
{
const char* savefilters[] = {
"*.exe"
};
outfile=tinyfd_saveFileDialog("select executable path", "", 1, savefilters);
if(outfile==nullptr)
{
exit(0);
}
}
}
if(compile)
{
int n = 0, t=0;
GlobalChunkInfo pkginfo = { 0, 0, 0, MAGIC };
chunkinfo e;
int bufferlen = 4096, bufferpos = 0;
char* buffer=malloc(bufferlen);
if(buffer==nullptr)
{
lua_pushstring(l, "unable to compile the script into executable!");
lua_error(l);
return 1;
}
fstream out(outfile, ios::binary|ios::out);
if(!out.is_open()||!in.is_open())
{
lua_pushstring(l, "unable to initialize executable compiler!");
lua_error(l);
return 1;
}
while(n=in.read(buffer, bufferlen).gcount()>0)
{
out.write(buffer, n);
}
in.close();
lua_getglobal(l, "string");
lua_getfield(l, -1, "dump");
unsigned long length=0;
const char* data=nullptr;
lua_pushvalue(l, -1);
luaL_loadfile(l, scriptfile);
pkginfo.count++;
preparename(scriptfile);
e.namelength=strlen(scriptfile)+1;
if(writechunk(l, &e, out, scriptfile, dbg))
{
lua_pushstring(l, "unable to write the executable to the disk");
lua_error(l);
return 1;
}
if (bufferpos+e.namelength+sizeof(e)+8 >= bufferlen)
{
buffer=realloc(buffer, bufferlen=max<int>(bufferlen*3/2 +1, bufferpos+e.namelength+sizeof(e)+16));
}
memcpy(buffer+bufferpos, &e, sizeof(e));
memcpy(buffer + bufferpos + sizeof(e), scriptfile, e.namelength);
bufferpos += e.namelength + sizeof(e);
pkginfo.offset=out.tellg();
pkginfo.length = bufferpos;
n=0; t=0;
while(t<bufferpos&&(n=out.write(buffer+t, bufferpos))>0)
{
t+=n;
}
if(t<bufferpos)
{
lua_pushstring(l, "unable to write into executable file!");
lua_error(l);
return 1;
}
out.flush();
out.close();
}
else
{
luaL_loadfile(l, scriptfile);
lua_pcall(l, 0, 0, 0);
}
return 0;
}
|