- append '\0' at the end of game name (dll injection)
- Release() fake texture (if force reload) instead of calling RemoveTexture(..)

Improvements:
- template support, you can now have more than one template set one of them as default.
- update function will only update the texture if this is needed or the reload button is pressed.
- textures will also be removed, if they are removed from the package list.
This commit is contained in:
code@koerner-de.net
2011-10-13 19:01:16 +00:00
parent ef57e3f86f
commit b2bade7ff8
34 changed files with 1544 additions and 562 deletions
+181 -66
View File
@@ -20,8 +20,11 @@ along with OpenTexMod. If not, see <http://www.gnu.org/licenses/>.
#include "OTM_Main.h"
OTM_Sender::OTM_Sender(PipeStruct &pipe, OTM_Language &lang) : Pipe(pipe), Language(lang)
OTM_Sender::OTM_Sender(PipeStruct &pipe) : Pipe(pipe)
{
OldTextures = NULL;
OldTexturesNum = 0;
try {Buffer = new char[BIG_BUFSIZE];}
catch (...) {Buffer=NULL;}
}
@@ -32,25 +35,25 @@ OTM_Sender::~OTM_Sender(void)
}
int OTM_Sender::Send( const OTM_GameInfo &game, const OTM_GameInfo &game_old, wxArrayString *comments)
int OTM_Sender::Send( const OTM_GameInfo &game, const OTM_GameInfo &game_old, bool force, wxArrayString *comments)
{
LastError.Empty();
int key = game.GetKeyBack();
if (key>=0 && key!=game_old.GetKeyBack())
{
key = Language.KeyValues[key];
key = Language->KeyValues[key];
SendKey( key, CONTROL_KEY_BACK);
}
key = game.GetKeySave();
if (key>=0 && key!=game_old.GetKeySave())
{
key = Language.KeyValues[key];
key = Language->KeyValues[key];
SendKey( key, CONTROL_KEY_SAVE);
}
key = game.GetKeyNext();
if (key>=0 && key!=game_old.GetKeyNext())
{
key = Language.KeyValues[key];
key = Language->KeyValues[key];
SendKey( key, CONTROL_KEY_NEXT);
}
@@ -80,8 +83,7 @@ int OTM_Sender::Send( const OTM_GameInfo &game, const OTM_GameInfo &game_old, wx
if (path!=game_old.GetSavePath()) SendPath(path);
// the rest of this function is not optimized !!
if (game.GetNumberOfFiles()<=0)
if (game.GetNumberOfFiles()<=0 && OldTexturesNum==0 && OldTextures==NULL)
{
if (LastError.Len()>0) return 1;
else return 0;
@@ -91,34 +93,145 @@ int OTM_Sender::Send( const OTM_GameInfo &game, const OTM_GameInfo &game_old, wx
game.GetFiles( files);
int num = files.GetCount();
if (comments!=NULL)
bool *checked = NULL;
if (num>0)
{
if (GetMemory(checked, num)) {LastError << Language->Error_Memory; return -1;}
game.GetChecked( checked, num);
}
AddTextureClass *tex = NULL;//new AddTextureClass[num+OldTexturesNum];
if (GetMemory( tex, num+OldTexturesNum)) {LastError << Language->Error_Memory; return -1;}
wxString comment;
if (force || OldTexturesNum==0 || OldTextures==NULL)
{
//reload everything
for (int i=0; i<num; i++)
{
OTM_File file( files[i]);
file.GetComment(comment);
tex[i].Comment = comment;
tex[i].Add = checked[i];
tex[i].Force = true;
tex[i].File = files[i];
if (file.GetContent( tex[i], checked[i]))
{
LastError << file.LastError;
file.LastError.Empty();
}
}
// append all packages, which was added but (maybe) are no longer in the list
int append = 0;
if (OldTexturesNum>0 && OldTextures!=NULL)
{
for (int i=0; i<OldTexturesNum; i++)
{
if (OldTextures[i].Add)
{
bool del = true;
for (int j=0; j<num; j++) if (OldTextures[i].File==tex[j].File) {del=false; break;}
if (del)
{
tex[num+append].InheriteMemory(OldTextures[i]);
tex[num+append].Add = false;
tex[num+append].Force = true;
append++;
}
}
}
}
SendTextures( num+append, tex);
}
else
{
//search for same packages to avoid reload from disk
//first step: maybe the order did not change
int pos = 0;
bool *hit=NULL;
if (GetMemory( hit, OldTexturesNum, false)) {LastError << Language->Error_Memory; return -1;}
for (int i=0; i<num && i<OldTexturesNum; i++)
{
if (OldTextures[i].File == files[i])
{
tex[i].InheriteMemory(OldTextures[i]);
tex[i].Force = false;
hit[i] = true;
}
else
{
pos = i;
break;
}
}
//second step: if the order changed -> looking brute force
for (int i=pos; i<num; i++) for (int j=pos; j<OldTexturesNum; j++)
{
if (!hit[j] && OldTextures[j].File == files[i])
{
tex[i].InheriteMemory(OldTextures[j]);
tex[i].Force = false;
hit[j] = true;
}
}
//next step, set Add to true or false and load packages, which are not loaded
for (int i=0; i<num; i++)
{
tex[i].Add = checked[i];
if (tex[i].Len==0 || (tex[i].Add && !tex[i].Loaded) )
{
OTM_File file( files[i]);
file.GetComment(comment);
tex[i].Comment = comment;
tex[i].Add = checked[i];
tex[i].Force = true;
tex[i].File = files[i];
file.GetContent( tex[i], tex[i].Add);
}
}
// append all packages, which was added but are no longer in the list
int append = 0;
if (OldTexturesNum!=0 && OldTextures!=NULL)
{
for (int j=pos; j<OldTexturesNum; j++)
{
if (!hit[j] && OldTextures[j].Add)
{
bool del = true;
for (int i=pos; i<num; i++) if (OldTextures[j].File==tex[i].File) {del=false; break;}
if (del)
{
tex[num+append].InheriteMemory(OldTextures[j]);
tex[num+append].Add = false;
tex[num+append].Force = true;
append++;
}
}
}
}
SendTextures( num+append, tex);
}
if (checked!=NULL) delete [] checked;
if (comments!=NULL && num>0)
{
comments->Empty();
comments->Alloc(num);
for (int i=0; i<num; i++) comments->Add(tex[i].Comment);
}
bool *checked = new bool [num];
game.GetChecked( checked, num);
AddTextureClass *tex = new AddTextureClass[num];
wxString comment;
for (int i=0; i<num; i++)
{
OTM_File file( Language, files[i]);
if (comments!=NULL)
{
file.GetComment(comment);
comments->Add(comment);
}
if (file.GetContent( &tex[i], checked[i], true))
{
LastError << file.LastError;
file.LastError.Empty();
}
}
SendTextures( num, tex);
if (OldTextures!=NULL) delete [] OldTextures;
delete [] checked;
delete [] tex;
OldTexturesNum = num;
OldTextures = tex;
if (LastError.Len()>0) return 1;
else return 0;
@@ -149,54 +262,56 @@ int OTM_Sender::SendSaveSingleTexture(bool val)
}
int OTM_Sender::SendTextures(unsigned int num, AddTextureClass *tex)
{
if (Buffer==NULL) return (RETURN_NO_MEMORY);
int count = 0;
for (unsigned int i=0u; i<num; i++) count+=tex[i].Num;
unsigned long *hash = new unsigned long[count];
count = 0;
MsgStruct *msg;
int pos = 0;
for (unsigned int i=0u; i<num; i++) for (unsigned int j=0u; j<tex[i].Num; j++)
{
bool hit = false;
unsigned long temp_hash = tex[i].Hash[j];
for (int h=0; h<count; h++) if (temp_hash == hash[h]) {hit= true; break;}
if (hit) continue;
if (tex[i].Size[j]+sizeof(MsgStruct)+pos>BIG_BUFSIZE)
if (tex[i].Force || !tex[i].Add || !tex[i].WasAdded[j])
// if force==true we must update
// tex[i].Add!=true we can always remove, cause removing does take time in the render thread
// if tex[i].Add==true and WasAdded[j]!=true this texture was not loaded but should be loaded, so maybe we can load it now
{
if (int ret = SendToGame( Buffer, pos)) return ret;
pos = 0;
}
bool hit = false;
unsigned long temp_hash = tex[i].Hash[j];
for (unsigned int ii=0u; ii<i && !hit; ii++) for (unsigned int jj=0u; jj<tex[ii].Num && !hit; jj++) if (temp_hash==tex[ii].Hash[jj]) hit=true;
for (unsigned int jj=0u; jj<j && !hit; jj++) if (temp_hash==tex[i].Hash[jj]) hit=true;
if (hit) continue;
hash[count++] = temp_hash;
unsigned int size = tex[i].Size[j];
msg = (MsgStruct*) &Buffer[pos];
msg->Hash = temp_hash;
msg->Value = size;
pos += sizeof(MsgStruct);
if (tex[i].Add[j])
{
if (tex[i].Force[j]) msg->Control = CONTROL_FORCE_RELOAD_TEXTURE_DATA;
else msg->Control = CONTROL_ADD_TEXTURE_DATA;
char* temp = tex[i].Textures[j];
if (temp!=NULL)
if (tex[i].Size[j]+sizeof(MsgStruct)+pos>BIG_BUFSIZE)
{
for (unsigned int l=0; l<size; l++) Buffer[pos+l] = temp[l];
pos+=size;
if (int ret = SendToGame( Buffer, pos)) return ret;
pos = 0;
}
unsigned int size = tex[i].Size[j];
msg = (MsgStruct*) &Buffer[pos];
msg->Hash = temp_hash;
msg->Value = size;
pos += sizeof(MsgStruct);
if (tex[i].Add)
{
msg->Control = CONTROL_FORCE_RELOAD_TEXTURE_DATA; //we always force because whether force==true
//or Add==true && WasAdded[j]!=true which means it is loaded the first time, or in previous loads it could not be loaded
//because an other texture was send with the same hash, in all cases forcing is the best choice (atm)
char* temp = tex[i].Textures[j];
if (temp!=NULL)
{
for (unsigned int l=0; l<size; l++) Buffer[pos+l] = temp[l];
pos+=size;
}
tex[i].WasAdded[j] = true;
}
else
{
msg->Control = CONTROL_REMOVE_TEXTURE;
tex[i].WasAdded[j] = false;
}
}
else msg->Control = CONTROL_REMOVE_TEXTURE;
}
delete [] hash;
if (pos) if (int ret = SendToGame( Buffer, pos)) return ret;
if (LastError.Len()>0) return 1;
@@ -252,10 +367,10 @@ int OTM_Sender::SendToGame( void *msg, unsigned long len)
if (len==0) return (RETURN_BAD_ARGUMENT);
unsigned long num;
if (Pipe.Out==INVALID_HANDLE_VALUE) {LastError << Language.Error_NoPipe; return -1;}
if (Pipe.Out==INVALID_HANDLE_VALUE) {LastError << Language->Error_NoPipe; return -1;}
bool ret = WriteFile( Pipe.Out, (const void*) msg, len, &num, NULL);
if (!ret || len!=num) {LastError << Language.Error_WritePipe; return -1;}
if (!FlushFileBuffers(Pipe.Out)) {LastError << Language.Error_FlushPipe; return -1;}
if (!ret || len!=num) {LastError << Language->Error_WritePipe; return -1;}
if (!FlushFileBuffers(Pipe.Out)) {LastError << Language->Error_FlushPipe; return -1;}
return 0;
}