Skip to content

Commit 61ce09b

Browse files
committed
Simplified luapack.Rename to remove path ID
Added lua/includes/init.lua and lua/send.txt renaming logic
1 parent 4be8b4d commit 61ce09b

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

source/main.cpp

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ namespace luapack
5757
static IFileSystem *filesystem = nullptr;
5858

5959
static std::unordered_set<std::string> whitelist_extensions = { "lua", "txt", "dat" };
60-
static std::unordered_set<std::string> whitelist_pathid = { "lsv", "lua", "data" };
6160

6261
class GModDataPackProxy : public Detouring::ClassProxy<GModDataPack, GModDataPackProxy>
6362
{
@@ -126,30 +125,20 @@ static bool IsPathAllowed( std::string &filename )
126125
return whitelist_extensions.find( ext ) != whitelist_extensions.end( );
127126
}
128127

129-
inline bool IsPathIDAllowed( std::string &pathid )
130-
{
131-
std::transform( pathid.begin( ), pathid.end( ), pathid.begin( ), [] ( uint8_t c )
132-
{
133-
return static_cast<char>( std::tolower( c ) );
134-
} );
135-
return whitelist_pathid.find( pathid ) != whitelist_pathid.end( );
136-
}
137-
138128
LUA_FUNCTION_STATIC( Rename )
139129
{
140130
LUA->CheckType( 1, GarrysMod::Lua::Type::STRING );
141131
LUA->CheckType( 2, GarrysMod::Lua::Type::STRING );
142-
LUA->CheckType( 3, GarrysMod::Lua::Type::STRING );
143132

144-
std::string fname = LUA->GetString( 1 ), fnew = LUA->GetString( 2 ), pathid = LUA->GetString( 3 );
133+
std::string fname = LUA->GetString( 1 ), fnew = LUA->GetString( 2 );
145134

146-
if( !IsPathAllowed( fname ) || !IsPathAllowed( fnew ) || !IsPathIDAllowed( pathid ) )
135+
if( !IsPathAllowed( fname ) || !IsPathAllowed( fnew ) )
147136
{
148137
LUA->PushBool( false );
149138
return 1;
150139
}
151140

152-
LUA->PushBool( filesystem->RenameFile( fname.c_str( ), fnew.c_str( ), pathid.c_str( ) ) );
141+
LUA->PushBool( filesystem->RenameFile( fname.c_str( ), fnew.c_str( ), "DATA" ) );
153142
return 1;
154143
}
155144

@@ -159,13 +148,41 @@ static void Initialize( GarrysMod::Lua::ILuaBase *LUA )
159148
if( filesystem == nullptr )
160149
LUA->ThrowError( "failed to initialize IFileSystem" );
161150

151+
if( filesystem->FileExists( "lua/includes/init.lua", "MOD" ) )
152+
{
153+
if( filesystem->FileExists( "lua/includes/_init.lua", "MOD" ) )
154+
{
155+
Msg( "[gmsv_luapack_internal] 'lua/includes/_init.lua' already exists, deleting it!\n" );
156+
filesystem->RemoveFile( "lua/includes/_init.lua", "MOD" );
157+
}
158+
159+
if( filesystem->RenameFile( "lua/includes/init.lua", "lua/includes/_init.lua", "MOD" ) )
160+
Msg( "[gmsv_luapack_internal] Renamed 'lua/includes/init.lua' to 'lua/includes/_init.lua'!\n" );
161+
else
162+
Warning( "[gmsv_luapack_internal] Failed to rename 'lua/includes/init.lua' to 'lua/includes/_init.lua'!\n" );
163+
}
164+
165+
if( filesystem->FileExists( "lua/send.txt", "MOD" ) )
166+
{
167+
if( filesystem->FileExists( "lua/_send.txt", "MOD" ) )
168+
{
169+
Msg( "[gmsv_luapack_internal] 'lua/_send.txt' already exists, deleting it!\n" );
170+
filesystem->RemoveFile( "lua/_send.txt", "MOD" );
171+
}
172+
173+
if( filesystem->RenameFile( "lua/send.txt", "lua/_send.txt", "MOD" ) )
174+
Msg( "[gmsv_luapack_internal] Renamed 'lua/send.txt' to 'lua/_send.txt'!\n" );
175+
else
176+
Warning( "[gmsv_luapack_internal] Failed to rename 'lua/send.txt' to 'lua/_send.txt'!\n" );
177+
}
178+
162179
GModDataPackProxy::Singleton.Initialize( LUA );
163180

164181
LUA->GetField( GarrysMod::Lua::INDEX_GLOBAL, "luapack" );
165182
if( !LUA->IsType( -1, GarrysMod::Lua::Type::TABLE ) )
166183
{
167-
GModDataPackProxy::Singleton.Deinitialize(LUA);
168-
LUA->ThrowError("luapack table not found");
184+
GModDataPackProxy::Singleton.Deinitialize( LUA );
185+
LUA->ThrowError( "luapack table not found" );
169186
}
170187

171188
LUA->PushCFunction( Rename );

0 commit comments

Comments
 (0)