Profile: add custom load/save hooks

Themes can use these to implement custom per-profile data, such as
SpeedMods.txt.  For memory cards, they will be executed while the card is
mounted and should read/write everything they will need at that point.
This commit is contained in:
Devin J. Pohly
2013-09-06 23:36:54 -04:00
parent 9309ee09e9
commit cc38fa1d5a
2 changed files with 47 additions and 1 deletions
+2
View File
@@ -1183,6 +1183,8 @@ JudgmentTransformCommand=%JudgmentTransformSharedCommand
[Profile]
ShowCoinData=true
UnlockAuthString=""
CustomLoadFunction=function(profile, dir) end
CustomSaveFunction=function(profile, dir) end
[RadarValues]
WriteComplexValues=true
+45 -1
View File
@@ -876,7 +876,31 @@ ProfileLoadResult Profile::LoadAllFromDir( RString sDir, bool bRequireSignature
return ProfileLoadResult_FailedTampered;
LOG->Trace( "Done." );
return LoadStatsXmlFromNode( &xml );
ProfileLoadResult ret = LoadStatsXmlFromNode(&xml);
if (ret != ProfileLoadResult_Success)
return ret;
/* Get the theme's custom load function:
* [Profile]
* CustomLoadFunction=function(profile, profileDir) ... end
*/
Lua *L = LUA->Get();
LuaReference customLoadFunc = THEME->GetMetricR("Profile", "CustomLoadFunction");
customLoadFunc.PushSelf(L);
ASSERT_M(!lua_isnil(L, -1), "CustomLoadFunction not defined");
// Pass profile and profile directory as arguments
this->PushSelf(L);
LuaHelpers::Push(L, sDir);
// Run it
RString sError;
if (!LuaHelpers::RunScriptOnStack(L, sError, 2, 0))
LOG->Warn("Error running CustomLoadFunction: %s", sError.c_str());
LUA->Release(L);
return ProfileLoadResult_Success;
}
ProfileLoadResult Profile::LoadStatsXmlFromNode( const XNode *xml, bool bIgnoreEditable )
@@ -936,6 +960,26 @@ bool Profile::SaveAllToDir( RString sDir, bool bSignData ) const
FILEMAN->CreateDir( sDir + SCREENSHOTS_SUBDIR );
FILEMAN->CreateDir( sDir + RIVAL_SUBDIR );
/* Get the theme's custom save function:
* [Profile]
* CustomSaveFunction=function(profile, profileDir) ... end
*/
Lua *L = LUA->Get();
LuaReference customSaveFunc = THEME->GetMetricR("Profile", "CustomSaveFunction");
customSaveFunc.PushSelf(L);
ASSERT_M(!lua_isnil(L, -1), "CustomSaveFunction not defined");
// Pass profile and profile directory as arguments
const_cast<Profile *>(this)->PushSelf(L);
LuaHelpers::Push(L, sDir);
// Run it
RString sError;
if (!LuaHelpers::RunScriptOnStack(L, sError, 2, 0))
LOG->Warn("Error running CustomSaveFunction: %s", sError.c_str());
LUA->Release(L);
return bSaved;
}