core/smn_core.cpp
changeset 2901 f5a1cf24f505
parent 2841 dde3dd0b4b8b
child 3027 0f90dd9754ca
     1.1 --- a/core/smn_core.cpp	Mon Jan 11 20:09:34 2010 -0800
     1.2 +++ b/core/smn_core.cpp	Mon Jan 11 22:46:44 2010 -0800
     1.3 @@ -673,6 +673,77 @@
     1.4  	return 1;
     1.5  }
     1.6  
     1.7 +enum NumberType
     1.8 +{
     1.9 +	NumberType_Int8,
    1.10 +	NumberType_Int16,
    1.11 +	NumberType_Int32
    1.12 +};
    1.13 +
    1.14 +//memory addresses below 0x10000 are automatically considered invalid for dereferencing
    1.15 +#define VALID_MINIMUM_MEMORY_ADDRESS 0x10000
    1.16 +
    1.17 +static cell_t LoadFromAddress(IPluginContext *pContext, const cell_t *params)
    1.18 +{
    1.19 +	void *addr = reinterpret_cast<void*>(params[1]);
    1.20 +
    1.21 +	if (addr == NULL)
    1.22 +	{
    1.23 +		pContext->ThrowNativeError("Address cannot be null");
    1.24 +	}
    1.25 +	else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
    1.26 +	{
    1.27 +		pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
    1.28 +	}
    1.29 +	NumberType size = static_cast<NumberType>(params[2]);
    1.30 +
    1.31 +	switch(size)
    1.32 +	{
    1.33 +	case NumberType_Int8:
    1.34 +		return *reinterpret_cast<uint8_t*>(addr);
    1.35 +	case NumberType_Int16:
    1.36 +		return *reinterpret_cast<uint16_t*>(addr);
    1.37 +	case NumberType_Int32:
    1.38 +		return *reinterpret_cast<uint32_t*>(addr);
    1.39 +	default:
    1.40 +		pContext->ThrowNativeError("Invalid number types %d", size);
    1.41 +	}
    1.42 +
    1.43 +	return 1;
    1.44 +}
    1.45 +
    1.46 +
    1.47 +static cell_t StoreToAddress(IPluginContext *pContext, const cell_t *params)
    1.48 +{
    1.49 +	void *addr = reinterpret_cast<void*>(params[1]);
    1.50 +
    1.51 +	if (addr == NULL)
    1.52 +	{
    1.53 +		pContext->ThrowNativeError("Address cannot be null");
    1.54 +	}
    1.55 +	else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
    1.56 +	{
    1.57 +		pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
    1.58 +	}
    1.59 +	cell_t data = params[2];
    1.60 +
    1.61 +	NumberType size = static_cast<NumberType>(params[3]);
    1.62 +
    1.63 +	switch(size)
    1.64 +	{
    1.65 +	case NumberType_Int8:
    1.66 +		*reinterpret_cast<uint8_t*>(addr) = data;
    1.67 +	case NumberType_Int16:
    1.68 +		*reinterpret_cast<uint16_t*>(addr) = data;
    1.69 +	case NumberType_Int32:
    1.70 +		*reinterpret_cast<uint32_t*>(addr) = data;
    1.71 +	default:
    1.72 +		pContext->ThrowNativeError("Invalid number types %d", size);
    1.73 +	}
    1.74 +
    1.75 +	return 1;
    1.76 +}
    1.77 +
    1.78  REGISTER_NATIVES(coreNatives)
    1.79  {
    1.80  	{"AutoExecConfig",			AutoExecConfig},
    1.81 @@ -699,6 +770,8 @@
    1.82  	{"VerifyCoreVersion",		VerifyCoreVersion},
    1.83  	{"GetFeatureStatus",        GetFeatureStatus},
    1.84  	{"RequireFeature",          RequireFeature},
    1.85 +	{"LoadFromAddress",         LoadFromAddress},
    1.86 +	{"StoreToAddress",          StoreToAddress},
    1.87  	{NULL,						NULL},
    1.88  };
    1.89