request: EraseFile()

Bug reports and feature requests. New features can only be added to the current development version. Bug-fixes may be back-ported.

Current release: 099 / Current development: 100
Post Reply
Terciob
Master Poster
Posts: 90
Joined: Fri Nov 07, 2008 3:47 am

request: EraseFile()

Post by Terciob »

We have writefile() and readfile(), but is missing something to erase those files.
A "EraseFile(file_string)" does delete requested file and his backup (.bak file created on writefile overwrite).

My goal with this request is create dynamic gump caches where instead process a entire gump (e.g. a craft skill) again and again and again, we can just store the full gump on a file with something like this:

Code: Select all

if (!readfile ("layout.txt"))
create entire gump layout...
and save_gump_cache (layout, data);
endif

var gump := senddialoggump (who, layout, data);

function save_gump_cache (layout, data)
	writefile ("layout.txt", layout);
	writefile ("data.txt", data);
endfunction 
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Re: request: EraseFile()

Post by CWO »

I've done an implementation of gump caching on my shard. I save all gumps to one datafile. If I need to remove the gump, I just delete the element.
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Re: request: EraseFile()

Post by CWO »

here's a bit of code that you might find useful...

Code: Select all

use datafile;

function CacheGump(group, name, layout, data)
	var gumpcache := OpenDataFile("::cachedgumps");
	if (!gumpcache)
		gumpcache  := CreateDataFile("::cachedgumps");
	endif
	var cacheelem := gumpcache.FindElement(group);
	if (!cacheelem)
		cacheelem := gumpcache.CreateElement(group);
	endif
	var gump := struct{};
	gump.layout := layout;
	gump.data := data;
	cacheelem.setprop(name, gump);
	UnloadDataFile("::cachedgumps");
endfunction

function RestoreCachedGump(group, name)
	UnloadDataFile("::cachedgumps");
	var gumpcache := OpenDataFile("::cachedgumps");
	if (!gumpcache)
		CreateDataFile("::cachedgumps");
		return 0;
	endif
	var cacheelem := gumpcache.FindElement(group);
	if (!cacheelem)
		return 0;
	endif
	var gump := cacheelem.getprop(name);
	if (!gump)
		return 0;
	endif
	if (gump.layout && gump.data)
		return gump;
	endif
	return 0;
endfunction
in your gump code:

Code: Select all

var gump := RestoreCachedGump(group, name);
if (!gump)
	//Build all unchanging items in your gump here.
	CacheGump(group, name, layout, data);
else
	layout := gump.layout;
	data := gump.data;
endif
//Build all variable items in your gump here
The group and name variables you pass can be whatever you think of. For instance I have group := "crafting"; and name := "tinkering". If you have multiple tools with different menus, the group could be named after the skill and the gump name could be the tool name, ect...

Example: RestoreCachedGump("crafting", "tinkering");
if it returns 0, it means you'll have to build the gump and subsequently call CacheGump("crafting", "tinkering", layout, data); to cache the gump under that group/name.


to delete it, I have a text command .deletegump

Code: Select all

use datafile;
use os;
use uo;

program deletegump(who, text)
	text := SplitWords(text);
	var gumpcache := OpenDataFile("::cachedgumps");
	if (!gumpcache)
		return;
	endif
	var cacheelem := gumpcache.FindElement(text[1]);
	if (!cacheelem)
		return;
	endif
	SendSysMessage(who, CStr(cacheelem.eraseprop(text[2])));
endprogram
usage: .deletegump crafting tinkering

that will delete the tinkering gump and send you '1' in a SysMessage or show you an error. Also remember, the group/name variables are case sensitive.
Post Reply