1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
4 #include "ConvertContext.h"
7 #include "UpToDateFileHelpers.h"
9 #include "LuaCompiler.h"
13 #include <Lua/src/lua.h>
14 #include <Lua/src/lauxlib.h>
16 #include <Lua/src/ldo.h>
17 #include <Lua/src/lfunc.h>
18 #include <Lua/src/lmem.h>
19 #include <Lua/src/lobject.h>
20 #include <Lua/src/lopcodes.h>
21 #include <Lua/src/lstring.h>
22 #include <Lua/src/lundump.h>
27 float script_frand0_1()
29 return rand() / static_cast<float>(RAND_MAX
);
32 void script_randseed(uint seed
)
38 // Shamelessly stolen from luac.c and modified a bit to support rc usage
40 #define toproto(L,i) (clvalue(L->top+(i))->l.p)
42 static const Proto
* combine(lua_State
* L
)
47 static int writer(lua_State
* L
, const void* p
, size_t size
, void* u
)
50 return (fwrite(p
,size
,1,(FILE*)u
)!=1) && (size
!=0);
53 static int pmain(lua_State
* L
)
55 LuaCompiler
* pCompiler
= reinterpret_cast<LuaCompiler
*>(lua_touserdata(L
, 1));
57 const char* filename
= pCompiler
->GetInFilename();
58 if (luaL_loadfile(L
,filename
)!=0)
60 RCLogError(lua_tostring(L
,-1));
65 if (pCompiler
->IsDumping())
67 const char* outputFilename
= pCompiler
->GetOutFilename();
69 FILE* D
= fopen(outputFilename
, "wb");
72 RCLogError("Cannot open %s", outputFilename
);
77 luaU_dump(L
, f
, writer
, D
, (int) pCompiler
->IsStripping(), (int) pCompiler
->IsBigEndian());
82 RCLogError("Cannot write to %s", outputFilename
);
88 RCLogError("Cannot close %s", outputFilename
);
96 //////////////////////////////////////////////////////////////////////////
97 LuaCompiler::LuaCompiler()
99 , m_bIsStripping(true)
100 , m_bIsBigEndian(false)
104 //////////////////////////////////////////////////////////////////////////
105 LuaCompiler::~LuaCompiler()
109 ////////////////////////////////////////////////////////////
110 string
LuaCompiler::GetOutputFileNameOnly() const
112 return PathUtil::RemoveExtension(m_CC
.sourceFileNameOnly
) + ".lua";
115 ////////////////////////////////////////////////////////////
116 string
LuaCompiler::GetOutputPath() const
118 return PathUtil::Make(m_CC
.GetOutputFolder(), GetOutputFileNameOnly());
121 //////////////////////////////////////////////////////////////////////////
122 bool LuaCompiler::Process()
124 string sourceFile
= m_CC
.GetSourcePath();
125 string outputFile
= GetOutputPath();
126 std::replace(sourceFile
.begin(), sourceFile
.end(), '/', '\\');
127 std::replace(outputFile
.begin(), outputFile
.end(), '/', '\\');
129 if (!m_CC
.bForceRecompiling
&& UpToDateFileHelpers::FileExistsAndUpToDate(GetOutputPath(), m_CC
.GetSourcePath()))
131 // The file is up-to-date
132 m_CC
.pRC
->AddInputOutputFilePair(m_CC
.GetSourcePath(), GetOutputPath());
138 const bool isPlatformBigEndian
= m_CC
.pRC
->GetPlatformInfo(m_CC
.platform
)->bBigEndian
;
141 m_bIsStripping
= true;
142 m_bIsBigEndian
= isPlatformBigEndian
;
143 m_sInFilename
= sourceFile
;
144 m_sOutFilename
= outputFile
;
146 lua_State
* L
= lua_open();
150 if (lua_cpcall(L
, pmain
, this) == 0)
156 RCLogError(lua_tostring(L
,-1));
163 RCLogError("Not enough memory for lua state");
168 if (!UpToDateFileHelpers::SetMatchingFileTime(GetOutputPath(), m_CC
.GetSourcePath()))
172 m_CC
.pRC
->AddInputOutputFilePair(m_CC
.GetSourcePath(), GetOutputPath());