Ticket #2475: MapSerialization_v1.0.diff

File MapSerialization_v1.0.diff, 5.4 KB (added by Yves, 10 years ago)

Serialization of Map objects combined with Sander's patch (except the serialization part in JS because it's obsolete now)

  • binaries/data/mods/public/simulation/components/Timer.js

     
    77{
    88    this.id = 0;
    99    this.time = 0;
    10     this.timers = {};
     10    this.timers = new Map();
    1111    this.turnLength = 0;
    1212};
    1313
     
    3333Timer.prototype.SetTimeout = function(ent, iid, funcname, time, data)
    3434{
    3535    var id = ++this.id;
    36     this.timers[id] = [ent, iid, funcname, this.time + time, 0, data];
     36    this.timers.set(id, [ent, iid, funcname, this.time + time, 0, data]);
    3737    return id;
    3838};
    3939
     
    5151    if (typeof repeattime != "number" || !(repeattime > 0))
    5252        error("Invalid repeattime to SetInterval of "+funcname);
    5353    var id = ++this.id;
    54     this.timers[id] = [ent, iid, funcname, this.time + time, repeattime, data];
     54    this.timers.set(id, [ent, iid, funcname, this.time + time, repeattime, data]);
    5555    return id;
    5656};
    5757
     
    6060 */
    6161Timer.prototype.CancelTimer = function(id)
    6262{
    63     delete this.timers[id];
     63    this.timers.delete(id);
    6464};
    6565
    6666
     
    7373    // Collect the timers that need to run
    7474    // (We do this in two stages to avoid deleting from the timer list while
    7575    // we're in the middle of iterating through it)
    76     var run = [];
    77     for (var id in this.timers)
     76    var run = new Set();
     77    for (let id of this.timers.keys())
    7878    {
    79         if (this.timers[id][3] <= this.time)
    80             run.push(id);
     79        if (this.timers.get(id)[3] <= this.time)
     80            run.add(id);
    8181    }
    82     for (var i = 0; i < run.length; ++i)
     82    for (let id of run)
    8383    {
    84         var id = run[i];
    85 
    86         var t = this.timers[id];
     84        var t = this.timers.get(id);
    8785        if (!t)
    8886            continue; // an earlier timer might have cancelled this one, so skip it
    8987
     
    9189        if (!cmp)
    9290        {
    9391            // The entity was probably destroyed; clean up the timer
    94             delete this.timers[id];
     92            this.timers.delete(id);
    9593            continue;
    9694        }
    9795
     
    110108            t[3] += t[4];
    111109            // Add it to the list to get re-executed if it's soon enough
    112110            if (t[3] <= this.time)
    113                 run.push(id);
     111                run.add(id);
    114112        }
    115113        else
    116114        {
    117115            // Non-repeating time - delete it
    118             delete this.timers[id];
     116            this.timers.delete(id);
    119117        }
    120118    }
    121119}
  • source/simulation2/serialization/BinarySerializer.cpp

     
    223223                m_Serializer.Bool("value", b);
    224224                break;
    225225            }
     226            else if (protokey == JSProto_Map)
     227            {
     228                // There's no C++ API (yet) to work with maps. This code relies on the internal
     229                // structure of the Iterator object returned by Map.entries. This is not ideal
     230                // because the structure could change in the future (and actually does change with v31)
     231                u32 mapSize;
     232                m_ScriptInterface.GetProperty(val, "size", mapSize);
     233               
     234                m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_MAP);
     235                m_Serializer.NumberU32_Unbounded("map size", mapSize);
     236               
     237                JS::RootedValue keyValueIterator(cx);
     238                m_ScriptInterface.CallFunction(val, "entries", &keyValueIterator);
     239                for(u32 i=0; i<mapSize; i++)
     240                {
     241                    JS::RootedValue keyValuePair(cx);
     242                    ENSURE(m_ScriptInterface.CallFunction(keyValueIterator, "next", &keyValuePair));
     243                   
     244                    JS::RootedObject keyValuePairObj(cx, &keyValuePair.toObject());
     245                   
     246                    JS::RootedValue key(cx);
     247                    JS::RootedValue value(cx);
     248                    ENSURE(JS_GetElement(cx, keyValuePairObj, 0, key.address()));
     249                    ENSURE(JS_GetElement(cx, keyValuePairObj, 1, value.address()));
     250                   
     251                    HandleScriptVal(key);
     252                    HandleScriptVal(value);
     253                }
     254               
     255                break;
     256            }
    226257            else
    227258            {
    228259                // Unrecognized class
  • source/simulation2/serialization/SerializedScriptTypes.h

     
    3434    SCRIPT_TYPE_OBJECT_PROTOTYPE = 11,  // user-defined prototype
    3535    SCRIPT_TYPE_OBJECT_NUMBER = 12,     // standard Number class
    3636    SCRIPT_TYPE_OBJECT_STRING = 13,     // standard String class
    37     SCRIPT_TYPE_OBJECT_BOOLEAN = 14     // standard Boolean class
     37    SCRIPT_TYPE_OBJECT_BOOLEAN = 14,    // standard Boolean class
     38    SCRIPT_TYPE_OBJECT_MAP = 15         // Map class
    3839};
    3940
    4041// ArrayBufferView subclasses (to avoid relying directly on the JSAPI enums)
  • source/simulation2/serialization/StdDeserializer.cpp

     
    389389
    390390        return JS::ObjectValue(*bufferObj);
    391391    }
     392    case SCRIPT_TYPE_OBJECT_MAP:
     393    {
     394        u32 mapSize;
     395        NumberU32_Unbounded("map size", mapSize);
     396        JS::RootedValue mapVal(cx);
     397        m_ScriptInterface.Eval("(new Map())", &mapVal);
     398       
     399        for(u32 i=0; i<mapSize; i++)
     400        {
     401            JS::RootedValue key(cx, ReadScriptVal("map key", JS::NullPtr()));
     402            JS::RootedValue value(cx, ReadScriptVal("map value", JS::NullPtr()));
     403            m_ScriptInterface.CallFunctionVoid(mapVal, "set", key, value);
     404        }
     405        AddScriptBackref(&mapVal.toObject());
     406        return mapVal;
     407    }
    392408    default:
    393409        throw PSERROR_Deserialize_OutOfBounds();
    394410    }