Beispiel #1
0
// save the context information for the script system
static BOOL eventSaveContext(char *pBuffer, UDWORD *pSize)
{
	UDWORD				size, valSize;
	SDWORD				numVars, i, numContext;
	SCRIPT_CONTEXT		*psCCont;
	VAL_CHUNK			*psCVals;
	INTERP_VAL			*psVal;
	SCR_VAL_SAVE		saveFunc;
	char				*pPos;
//not hashed	char				*pScriptID;
	UDWORD				hashedName;
	UWORD				*pValSize = NULL;


	size = 0;
	numContext = 0;
	pPos = pBuffer;

	// reserve space to store how many contexts are saved
	if (pBuffer != NULL)
	{
		pPos += sizeof(SWORD);
	}
	size += sizeof(SWORD);

	// go through the context list
	for(psCCont = psContList; psCCont != NULL; psCCont = psCCont->psNext)
	{
		numContext += 1;

		// save the context info
//nothashed if (!resGetIDfromData("SCRIPT", psCCont->psCode, &hashedName))
		if (!resGetHashfromData("SCRIPT", psCCont->psCode, &hashedName))
		{
			debug( LOG_FATAL, "eventSaveContext: couldn't find script resource id" );
			abort();
			return false;
		}
		numVars = psCCont->psCode->numGlobals + psCCont->psCode->arraySize;

		if (pBuffer != NULL)
		{
//not hashed			strcpy(pPos, pScriptID);
//not hashed			pPos += strlen(pScriptID) + 1;
			*((UDWORD*)pPos) = (UDWORD)hashedName;
			endian_udword((UDWORD*)pPos);
			pPos += sizeof(UDWORD);

			*((SWORD*)pPos) = (SWORD)numVars;
			endian_sword((SWORD*)pPos);
			pPos += sizeof(SWORD);

			*pPos = (UBYTE)psCCont->release;
			pPos += sizeof(UBYTE);
		}

//not hashed		size += strlen(pScriptID) + 1 + sizeof(SWORD) + sizeof(UBYTE);
		size += sizeof(UDWORD) + sizeof(SWORD) + sizeof(UBYTE);

		// save the context variables
		for(psCVals = psCCont->psGlobals; psCVals != NULL; psCVals = psCVals->psNext)
		{
			for(i=0; i < CONTEXT_VALS; i+= 1)
			{
				psVal = psCVals->asVals + i;

				// store the variable type
				if (pBuffer != NULL)
				{
					ASSERT( psVal->type < SWORD_MAX,
						"eventSaveContext: variable type number too big" );

					*((SWORD*)pPos) = (SWORD)psVal->type;
					endian_sword((SWORD*)pPos);

					pPos += sizeof(SWORD);
				}
				size += sizeof(SWORD);

				// store the variable value
				if (psVal->type == VAL_STRING)
				{
					UDWORD stringLen = 0;

					if(psVal->v.sval != NULL && strlen(psVal->v.sval) > 0)
					{
						stringLen = strlen(psVal->v.sval) + 1;
					}

					if (pBuffer != NULL)
					{
						*((UDWORD *)pPos) = stringLen;
						endian_udword((UDWORD *)pPos);
						pPos += sizeof(UDWORD);

						if(stringLen > 0)
						{
							strcpy((char *)pPos, psVal->v.sval);
						}
						pPos += stringLen;
					}

					size += sizeof(UDWORD) + stringLen;
				}
				else if (psVal->type < VAL_USERTYPESTART)
				{
					// internal type
					if (pBuffer != NULL)
					{
/* FIXME: this does not work for VAL_OBJ_GETSET, VAL_FUNC_EXTERN */
						*((UDWORD *)pPos) = (UDWORD)psVal->v.ival;
						endian_udword((UDWORD*)pPos);
						
						pPos += sizeof(UDWORD);
					}

					size += sizeof(UDWORD);
				}
				else
				{
					// user defined type
					saveFunc = asScrTypeTab[psVal->type - VAL_USERTYPESTART].saveFunc;

					ASSERT( saveFunc != NULL,
						"eventSaveContext: no save function for type %d\n", psVal->type );

					// reserve some space to store how many bytes the value uses
					if (pBuffer != NULL)
					{
						pValSize = (UWORD *)pPos;
						pPos += sizeof(UWORD);
					}
					size += sizeof(UWORD);

					if (!saveFunc(psVal, pPos, &valSize))
					{
						debug( LOG_FATAL, "eventSaveContext: couldn't get variable value size" );
						abort();
						return false;
					}

					if (pBuffer != NULL)
					{
						*pValSize = (UWORD)valSize;
						endian_uword((UWORD*)pValSize);

						pPos += valSize;
					}
					size += valSize;
				}

				numVars -=1;
				if (numVars <= 0)
				{
					// done all the variables
					ASSERT( psCVals->psNext == NULL,
						"eventSaveContext: number of context variables does not match the script code" );
					break;
				}
			}
		}
		ASSERT( numVars == 0,
			"eventSaveContext: number of context variables does not match the script code" );
	}

	// actually store how many contexts have been saved
	if (pBuffer != NULL)
	{
		*((SWORD *)pBuffer) = (SWORD)numContext;
		endian_sword((SWORD*)pBuffer);
	}
	*pSize = size;

	return true;
}
Beispiel #2
0
// save the context information for the script system
static bool eventSaveContext(WzConfig &ini)
{
    int numVars, numContext = 0;
    UDWORD hashedName;

    // go through the context list
    for (SCRIPT_CONTEXT *psCCont = psContList; psCCont != NULL; psCCont = psCCont->psNext)
    {
        // save the context info
        if (!resGetHashfromData("SCRIPT", psCCont->psCode, &hashedName))
        {
            debug(LOG_FATAL, "Could not find script resource id");
            return false;
        }
        numVars = psCCont->psCode->numGlobals + psCCont->psCode->arraySize;

        ini.beginGroup("context_" + QString::number(numContext));
        ini.setValue("context", hashedName);
        ini.setValue("numVars", numVars);
        ini.setValue("release", psCCont->release);
        ini.beginGroup("var");

        // save the context variables
        int countVar = 0;
        for (VAL_CHUNK *psCVals = psCCont->psGlobals; psCVals != NULL; psCVals = psCVals->psNext)
        {
            for (int i = 0; i < CONTEXT_VALS; i++)
            {
                INTERP_VAL *psVal = psCVals->asVals + i;

                ASSERT(psVal->type < SWORD_MAX, "Variable type number %d too big", (int)psVal->type);

                ini.beginGroup(QString::number(countVar));
                ini.setValue("type", QVariant(psVal->type));
                ini.setValue("typename", QString(scriptTypeToString(psVal->type))); // for debugging

                // store the variable value
                if (psVal->type == VAL_STRING)
                {
                    ini.setValue("data", QString(psVal->v.sval));
                }
                else if (psVal->type == VAL_BOOL)
                {
                    ini.setValue("data", QVariant((bool)psVal->v.bval));
                }
                else if (psVal->type == VAL_FLOAT)
                {
                    ini.setValue("data", QVariant((float)psVal->v.fval));
                }
                else if (psVal->type == VAL_OBJ_GETSET || psVal->type == VAL_FUNC_EXTERN)
                {
                    ini.setValue("data", QString("n/a"));
                }
                else if (psVal->type < VAL_USERTYPESTART)
                {
                    ini.setValue("data", QVariant(psVal->v.ival));
                }
                else
                {
                    // user defined type
                    SCR_VAL_SAVE saveFunc = asScrTypeTab[psVal->type - VAL_USERTYPESTART].saveFunc;

                    ASSERT(saveFunc != NULL, "No save function for type %d", psVal->type);

                    if (!saveFunc(psVal, ini))
                    {
                        debug(LOG_FATAL, "Could not get user defined variable value");
                        return false;
                    }
                }

                numVars -=1;
                countVar++;
                ini.endGroup();
                if (numVars <= 0)
                {
                    // done all the variables
                    ASSERT(psCVals->psNext == NULL, "Number of context variables does not match the script code");
                    break;
                }
            }
        }
        ASSERT(numVars == 0, "Number of context variables does not match the script code (%d)", numVars);
        ini.endGroup();
        ini.endGroup();
        numContext++;
    }

    // actually store how many contexts have been saved
    ini.setValue("general/contexts", QVariant(numContext));

    return true;
}