Allow scripting of instances w/ persistent data

Some fixes for TC and changes overall

Pass map object to hook via function arguments

The map object is no longer stored inside the instance data table.

Fix mistake in base64 decoder

It was failing whenever it encountered a '=' character, which is
completely valid.

Make ElunaInstanceAI::Load always load something

When it failed to load data, it was leaving nothing on the stack. Since
subsequent code expected Load to always load something, this was causing
issues.

Now, when Load fails to load anything, it just leaves a new empty table on
the stack.

Also: the error messages for Load have been improved.

Modify lua-marshal to allow saving of functions/userdata.

Some additional code was needed to save functions due to the inclusion of
a reference to _ENV within their upvalues (since Lua 5.2).

During encoding, a placeholder is left where the _ENV reference would be.
During decoding, a reference to the current _G is swapped with the
placeholder.

Make ElunaInstanceAI::Load re-initialize if data failed to load.

Also improve error messages by not including the raw data.

Improve storage format of upvalues

Instead of storing the upvalues by name, store by index. A wrapper is
still used in case the upvalue is nil, to prevent holes in the upvalues table.

A special field in the upvalues table, "E", is used to store the index of
the _ENV reference (if there was one). A reference to the current globals
table is set as the upvalue upon decoding.

Remove wrapping from upvalue storing, instead save amount of upvalues
This commit is contained in:
Patman64
2015-01-18 21:53:50 -05:00
committed by Rochet2
parent d57ca139b7
commit 31470c2cf7
14 changed files with 1564 additions and 25 deletions

151
ElunaInstanceAI.h Normal file
View File

@@ -0,0 +1,151 @@
/*
* Copyright (C) 2010 - 2015 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#ifndef _ELUNA_INSTANCE_DATA_H
#define _ELUNA_INSTANCE_DATA_H
#include "LuaEngine.h"
#ifdef TRINITY
#include "InstanceScript.h"
#else
#include "InstanceData.h"
#endif
/*
* This class is a small wrapper around `InstanceData`,
* allowing instances to be scripted with Eluna.
*
*
* Note 1
* ======
*
* Instances of `ElunaInstanceAI` are owned by the core, so they
* are not deleted when Eluna is reloaded. Thus `Load` is only called
* by the core once, no matter how many times Eluna is reloaded.
*
* However, when Eluna reloads, all instance data in Eluna is lost.
* So the solution is as follows:
*
* 1. Store the last save data in the member var `lastSaveData`.
*
* At first this is just the data given to us by the core when it calls `Load`,
* but later on once we start saving new data this is from Eluna.
*
* 2. When retrieving instance data from Eluna, check if it's missing.
*
* The data will be missing if Eluna is reloaded, since a new Lua state is created.
*
* 3. If it *is* missing, call `Reload`.
*
* This reloads the last known instance save data into Eluna, and calls the appropriate hooks.
*
*
* Note 2
* ======
*
* CMaNGOS expects some of these methods to be `const`. However, any of these
* methods are free to call `Save`, resulting in mutation of `lastSaveData`.
*
* Therefore, none of the hooks are `const`-safe, and `const_cast` is used
* to escape from these restrictions.
*/
class ElunaInstanceAI : public InstanceData
{
private:
// The last save data to pass through this class,
// either through `Load` or `Save`.
std::string lastSaveData;
// Used to prevent saving this AI while it's still loading,
// effectively nuking the data that's supposed to be loaded.
// Set to `false` while loading, `true` otherwise.
bool canSave;
public:
ElunaInstanceAI(Map* map) : InstanceData(map), canSave(true)
{
}
void Initialize() override;
/*
* These are responsible for serializing/deserializing the instance's
* data table to/from the core.
*/
void Load(const char* data) override;
#ifdef TRINITY
// Simply calls Save, since the functions are a bit different in name and data types on different cores
std::string GetSaveData() override
{
return Save();
}
const char* Save() const;
#else
const char* Save() const override;
#endif
/*
* Calls `Load` with the last save data that was passed to
* or from Eluna.
*
* See: big documentation blurb at the top of this class.
*/
void Reload()
{
Load(NULL);
}
/*
* These methods allow non-Lua scripts (e.g. DB, C++) to get/set instance data.
*/
uint32 GetData(uint32 key) const override;
void SetData(uint32 key, uint32 value) override;
uint64 GetData64(uint32 key) const override;
void SetData64(uint32 key, uint64 value) override;
/*
* These methods are just thin wrappers around Eluna.
*/
void Update(uint32 diff) override
{
// If Eluna is reloaded, it will be missing our instance data.
// Reload here instead of waiting for the next hook call (possibly never).
// This avoids having to have an empty Update hook handler just to trigger the reload.
if (!sEluna->HasInstanceData(instance))
Reload();
sEluna->OnUpdateInstance(this, diff);
}
bool IsEncounterInProgress() const override
{
return sEluna->OnCheckEncounterInProgress(const_cast<ElunaInstanceAI*>(this));
}
void OnPlayerEnter(Player* player) override
{
sEluna->OnPlayerEnterInstance(this, player);
}
#ifdef TRINITY
void OnGameObjectCreate(GameObject* gameobject) override
#else
void OnObjectCreate(GameObject* gameobject) override
#endif
{
sEluna->OnGameObjectCreate(this, gameobject);
}
void OnCreatureCreate(Creature* creature) override
{
sEluna->OnCreatureCreate(this, creature);
}
};
#endif // _ELUNA_INSTANCE_DATA_H