Zippy-Egoboo Home EgoWiki > Documentation > GameSavingPlans (r1.1 vs. r1.7) EgoWiki webs:
Main | TWiki | Know | Sandbox
Documentation . { Changes | Index | Search | Go }
 <<O>>  Difference Topic GameSavingPlans (r1.7 - 03 Nov 2004 - ElminI)
Changed:
<
<

Last two continued for all fields of table
>
>

Continues in (key, value) pairs for all instance data
Added:
>
>

Range:

Kind Size Description
Data 4 ADATA_TAG
Data int D_VECTOR
Data int LUA_REFNIL if the range is unreferenced, or some other value if it is referenced
Data float The minimum value in the range
Data float The maximum value in the range
Data 1 A bitset in which the lowest bit is set iff the range has a minimum value and the second-lowest is set iff it has a maximum value

List:

A list will be stored from Node** (or, equivalently, List*) to the following representation:

Kind Size Description
Data 4 ADATA_TAG
Data int D_LIST (yet to be created, corresponds to List* as defined in lualist.h)
Data 1 Nonzero if the list has an Instance
Reference N/A To the Instance (not present if the previous data is zero)
Reference N/A To the first item
Reference N/A To the second item
Continues for all list items

Class:

Kind Size Description
Data 4 ADATA_TAG
Data int D_CLASS
Data int LUA_NOREF unless the class is referenced by C code
Data int Sequence number in file
Data ? String filename of file the class was defined in
Reference N/A To the MTable of members
Reference N/A To the class data (if non-NULL)

Instance:

Kind Size Description
Data 4 ADATA_TAG
Data int D_INSTANCE
Data int LUA_NOREF unless the instance is referenced by C code
Reference N/A To the class the instance was instantiated from
Reference N/A To the MTable of members
Reference N/A To the class for the first instance data
Reference N/A To the first instance data
Reference N/A To the class for the second instance data
Reference N/A To the second instance data
Continues in (class, data) pairs for all instance data

 <<O>>  Difference Topic GameSavingPlans (r1.6 - 22 Oct 2004 - ElminI)
Added:
>
>

get_object_data() and reset_object_data()

The savefile framework provides two functions for accessing object data. The first function provides a simple iterator over the data segments in an object, returning the first datum first, the second next, and NULL after the last segment has been returned. reset_object_data() can be used to put start the process over again with the first datum.

The conventional use of these functions in the restoring of a particular data type is as follows:

  • Call reset_object_data() first. Sometimes other restore functions will
read data from an object to find out what type it is, and this call ensures that you always start with a full set of data.
  • Call get_object_data() in order for each data segment needed to restore
the object.

Note: The memory pointed to by the return values of get_object_data() are freed after the current savefile is restored. You should never save these pointers for use outside the scope of the restore function, and you should never attempt to free them.

Deleted:
<
<

Note: not implemented yet, subject to change.

Changed:
<
<

Lua values and those containing some other kind of values. To differentiate,

>
>

Lua values and those containing some other kind of value. To differentiate,

Changed:
<
<

ADATA_TAG, which should be compared to the tag interpreted as an integer. A similar constant is forthcoming for lua values.

>
>

ADATA_TAG, which should be compared to the tag interpreted as an integer. An analogous constant, LUA_TAG, is defined by luasave.h.


 <<O>>  Difference Topic GameSavingPlans (r1.5 - 21 Oct 2004 - ElminI)
Changed:
<
<

-- ElminI - 24 Sep 2004

>
>

Note: the integers given above correspond to different binary representations depending on the endian-ness of the platform. adatasave.h defines a constant ADATA_TAG, which should be compared to the tag interpreted as an integer. A similar constant is forthcoming for lua values.

C Structure Representations

For reference, here are the representations used for various C structures in Zippy. Note that all structures begin with the ADATA_TAG mentioned above, followed by a constant from the DataType enum defined in luascript/data.h.

Vector:

Kind Size Description
Data 4 ADATA_TAG
Data int D_VECTOR
Data int LUA_REFNIL if the vector is unreferenced, or some other value if it is referenced
Data float The x component of the vector
Data float The y component of the vector
Data float The z component of the vector

-- ElminI - 21 Oct 2004


 <<O>>  Difference Topic GameSavingPlans (r1.4 - 04 Oct 2004 - ElminI)
Added:
>
>

restore_first() and read_reference_restoring_first()

Caution must be excercised in calling the above-mentioned functions. The general rule is that when a particular object is being restored, as much information as possible should be retreived by other methods and stored before one of these functions is called. For example, the function to restore a Lua table must call read_reference_restoring_first(), but it does so only after creating a Lua reference for the table and storing it in the allocated memory. This is so that other objects which refer cyclically to the table will be able to work with this reference without referring to uninitialized memory.


 <<O>>  Difference Topic GameSavingPlans (r1.3 - 28 Sep 2004 - ElminI)
Added:
>
>

Object Headers

Saved objects fall somewhat arbitrarily into two categories: those containing Lua values and those containing some other kind of values. To differentiate, each object should start with one of two four-byte tags, "LUA\0" or "DATA", corresponding to the integers 0x4C554100 or 0x44415441.


 <<O>>  Difference Topic GameSavingPlans (r1.2 - 25 Sep 2004 - ElminI)
Added:
>
>

Savefile Representations of Lua Data

For your reference, here's how Lua data is represented in the savefile. As you can see, at this point every Lua value starts with the string, "LUA". I may change this as the framework matures.

Nil:

Kind Size Description
Data 4 The string "LUA"
Data int LUA_TNIL

Number:

Kind Size Description
Data 4 The string "LUA"
Data int LUA_TNUMBER
Data float The number

String:

Kind Size Description
Data 4 The string "LUA"
Data int LUA_TSTRING
Data n The string

Table:

Kind Size Description
Data 4 The string "LUA"
Data int LUA_TTABLE
Reference N/A Key of first field, always to a Lua value
Reference N/A Value of first field, always to a Lua value
Last two continued for all fields of table

Userdata:

Note: not implemented yet, subject to change.

Kind Size Description
Data 4 The string "LUA"
Data int LUA_TUSERDATA
Reference N/A To the C structure

 <<O>>  Difference Topic GameSavingPlans (r1.1 - 24 Sep 2004 - ElminI)
Added:
>
>

%META:TOPICINFO{author="ElminI" date="1096061815" format="1.0" version="1.1"}% %META:TOPICPARENT{name="WebHome"}%

Plans for Allowing Players to Save Their Games

At least for now, we are going to attempt to allow saving of the game by a full dump of state information, including lua data, to file. This will be difficult, however, since Zippy's state includes data maintained by both C and Lua code, and since it involves many pointers and reference loops. To help with this, I've made up a simple framework for saving such data to file and restoring it.

The Savefile Framework

The file savefile.h defines functions for writing savefiles and reading them. In this scheme, data to be saved is divided up into "objects," which consist of two kinds of information: references to other objects, and strings of binary data. References are simply C pointers to sections of memory which represent the objects they refer to in some fashion.

When a savefile is written, the savefile framework keeps track of the size of each object in memory. It interns objects, assigns them index numbers, and translates references accordingly.

When a savefile is read, the framework allocates the memory for each object according to the sizes given in the writing stage and resolves the references back to C pointers. It then calls a callback for each object, which is responsible for determining the type of the object and restoring it using the references and data that were stored with it.

Representing Lua Data

The mapping of C structures to the framework above is relatively simple, but this is not the case for Lua data, which is not normally referred to using C pointers. The file luasave.h defines functions that save and restore lua data. The method employed is to intern each object to be saved, mapping it to a C pointer to an integer, which is itself a canonical Lua reference to the object. This pointer is used within the savefile framework to represent the object and to refer to it.

Interacting with Saved Lua Data

If a C structure is being saved, and it needs to store a reference to some Lua data, the appropriate method is as follows:

  1. Push the Lua value onto the stack.
  2. Call save_lua_value() on it.

This will add the Lua data to the file and associate a reference to it with the C structure in the savefile. When the C structure is to be restored, the appropriate method of retreiving the Lua reference is as follows:

  1. Call read_reference_restoring_first() to restore the Lua data and retreive a C pointer to a Lua reference to it.
  2. Call lua_getref() to push the Lua data onto the stack.
  3. Make a new Lua reference to the data. Do NOT save or refer to the pointer or Lua reference returned by read_reference_restoring_first(), as both will be released when the restoring process is complete.

-- ElminI - 24 Sep 2004


Topic GameSavingPlans . { View | Diffs | r1.7 | > | r1.6 | > | r1.5 | More }
Revision r1.1 - 24 Sep 2004 - 21:36 GMT - ElminI
Revision r1.7 - 03 Nov 2004 - 22:21 GMT - ElminI
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding EgoWiki? Send feedback.