!I (1670414, 1670415, 1670416, 1670424, 1670431):
[CRYENGINE.git] / Code / Tools / RC / ResourceCompilerPC / LuaCompiler.cpp
blobc1dd8d138da49bdf55756431c76fbce5b7cf3498
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #include "StdAfx.h"
4 #include "ConvertContext.h"
5 #include "IConfig.h"
6 #include "FileUtil.h"
7 #include "UpToDateFileHelpers.h"
9 #include "LuaCompiler.h"
11 extern "C"
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>
25 extern "C"
27 float script_frand0_1()
29 return rand() / static_cast<float>(RAND_MAX);
32 void script_randseed(uint seed)
34 srand(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)
44 return toproto(L,-1);
47 static int writer(lua_State* L, const void* p, size_t size, void* u)
49 UNUSED(L);
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));
56 const Proto* f;
57 const char* filename = pCompiler->GetInFilename();
58 if (luaL_loadfile(L,filename)!=0)
60 RCLogError(lua_tostring(L,-1));
61 return 1;
64 f = combine(L);
65 if (pCompiler->IsDumping())
67 const char* outputFilename = pCompiler->GetOutFilename();
69 FILE* D = fopen(outputFilename, "wb");
70 if (D == NULL)
72 RCLogError("Cannot open %s", outputFilename);
73 return 1;
76 lua_lock(L);
77 luaU_dump(L, f, writer, D, (int) pCompiler->IsStripping(), (int) pCompiler->IsBigEndian());
78 lua_unlock(L);
80 if (ferror(D))
82 RCLogError("Cannot write to %s", outputFilename);
83 return 1;
86 if (fclose(D))
88 RCLogError("Cannot close %s", outputFilename);
89 return 1;
93 return 0;
96 //////////////////////////////////////////////////////////////////////////
97 LuaCompiler::LuaCompiler()
98 : m_bIsDumping(true)
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());
133 return true;
136 bool ok = false;
138 const bool isPlatformBigEndian = m_CC.pRC->GetPlatformInfo(m_CC.platform)->bBigEndian;
140 m_bIsDumping = true;
141 m_bIsStripping = true;
142 m_bIsBigEndian = isPlatformBigEndian;
143 m_sInFilename = sourceFile;
144 m_sOutFilename = outputFile;
146 lua_State* L = lua_open();
148 if (L)
150 if (lua_cpcall(L, pmain, this) == 0)
152 ok = true;
154 else
156 RCLogError(lua_tostring(L,-1));
159 lua_close(L);
161 else
163 RCLogError("Not enough memory for lua state");
166 if (ok)
168 if (!UpToDateFileHelpers::SetMatchingFileTime(GetOutputPath(), m_CC.GetSourcePath()))
170 return false;
172 m_CC.pRC->AddInputOutputFilePair(m_CC.GetSourcePath(), GetOutputPath());
175 return ok;