Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 02-13-2009, 01:16 PM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
Mass text replacer in files

This is a tool I made to replace filenames in levels on a large scale, considering half of Maloria's levels were filled with (what I considered) badly named files.
  • Uses
    • Replace filenames
    • Replace links
    • etc...

For example, I wanted to replace all occurrences of the text "malinn_sign.gif" from all levels with the text "mal_malinn-sign.gif":

PHP Code:
replaceTxt("malinn-sign.gif""mal_malinn-sign.gif"); 
Starting scan on 1903 files...
Replaced text 1 times in levels/levels/overworld/mal-g06.nw!
Done! Replaced text a total of 1 times!

  • In order for the script to work in all instances, I had to make a check so it won't replace the sent text if it's found with "mal_" in front of it. This is to avoid a never ending while loop that would break the script and cause major server lag. I suggest changing temp.header to suit your needs.
  • It will only alter the first occurrence of the file found, so if you have duplicate names, it won't touch more then one of them.

For this to work, you need to give (npcserver) rw to the submitted folder, as well as it's subfolders.

PHP Code:
function replaceTxt(prepast)
{
  
temp.header "mal_"// Anti never ending while (...) loop
  
temp.allowedExt = { ".nw" }; // Will skip all files but these
  
temp.folderPath "levels/levels/"// Base folder
  
temp.checkSubs true// Scan through subfolders too?

  
temp.folder.loadFolder(temp.folderPath "*"temp.checkSubs);
  echo(
"Starting scan on" SPC temp.folder.size() SPC "files...");
  
  
temp.var2 NULL;
  for (
temp.filetemp.folder) {
    if (!(
extractfileext(temp.filein temp.allowedExt)) continue;
    
temp.file findfiles(extractfilename(temp.file), true)[0];
    
    
temp.lines "";
    
temp.lines.loadString(temp.file);
    
    
temp.var = NULL;
    while (
temp.lines.pos(pre) > -1) {
      
temp.pos temp.lines.pos(pre);
      if (
temp.lines.substring(temp.pos temp.header.length(), temp.header.length()) == temp.header) break;
      
      
temp.lines temp.lines.substring(0temp.pos) @ past temp.lines.substring(temp.pos pre.length());
      
temp.var ++;
      
temp.var2 ++;
    }
    
    if (
temp.var != NULL) {
      echo(
"Replaced text" SPC temp.var SPC "times in" SPC temp.file "!");
      
temp.lines.savestring(temp.filefalse);
    }
  }
  echo(
"Done! Replaced text a total of" SPC temp.var2 SPC "times!");


__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto

Last edited by xXziroXx; 02-13-2009 at 06:11 PM..
Reply With Quote
  #2  
Old 02-13-2009, 04:37 PM
James James is offline
LOCAL DOUCHEBAG
James's Avatar
Join Date: Aug 2001
Location: Oregon
Posts: 3,132
James is a splendid one to beholdJames is a splendid one to beholdJames is a splendid one to beholdJames is a splendid one to behold
Send a message via AIM to James
Nice job, this is awesome.
__________________

JAMES BAY PACKERS
2011 FANTASY FOOTBALL CHAMPIONS
2012 FANTASY FOOTBALL CHAMPIONS


“If I can slow it down in my mind... things will be fine."

-Aaron Rodgers
2011's NFL Most Valuable Player




Reply With Quote
  #3  
Old 02-13-2009, 11:11 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Awesome, thanks!
__________________
Reply With Quote
  #4  
Old 02-15-2009, 03:19 PM
[email protected] sid.gottlieb@googlemail.com is offline
Banned
Join Date: Mar 2008
Posts: 861
sid.gottlieb@googlemail.com will become famous soon enough
v.cool i like
Reply With Quote
  #5  
Old 10-11-2011, 09:06 AM
xXziroXx xXziroXx is offline
Master of Puppets
xXziroXx's Avatar
Join Date: May 2004
Location: Sweden
Posts: 5,288
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Send a message via AIM to xXziroXx Send a message via MSN to xXziroXx
I don't like looking back and seeing my Code Library entries being really outdated and some not even working anymore, so I'll start updating them I suppose!

To start off, I've made this function more reliable in it's behaviour. The old code tended to randomly skip text to replace.

PHP Code:
function replaceTxt(prepast)
{
  
temp.header "mal_"// Anti never ending while (...) loop
  
temp.allowedExt = { ".nw" }; // Will skip all files but these
  
temp.folderPath "levels/levels/"// Base folder
  
temp.checkSubs true// Scan through subfolders too?
  
  
temp.folder.loadFolder(temp.folderPath "*"temp.checkSubs);
  echo(
"Starting scan on" SPC temp.folder.size() SPC "files...");
  
  
temp.totalLevelChanges NULL;
  for (
temp.filetemp.folder) {
    if (!(
extractfileext(temp.filein temp.allowedExt))
      continue;
    
    
temp.file findfiles(extractfilename(temp.file), true)[0];
    
temp.lines "";
    
temp.lines.loadString(temp.file);
    
    
// Times replaced this level
    
temp.currentLevelChanges NULL;
    while (
temp.lines.pos(pre) > -1) {
      
temp.pos temp.lines.pos(pre);
      if (
temp.lines.substring(temp.pos temp.header.length(), temp.header.length()) == temp.header)
        break;
            
      
temp.lines temp.lines.substring(0temp.pos) @ past temp.lines.substring(temp.pos pre.length());
      
temp.currentLevelChanges ++;
      
temp.totalLevelChanges ++;
    }
    
    if (
temp.currentLevelChanges != NULL) {
      echo(
"Replaced text" SPC temp.currentLevelChanges SPC "times in" SPC temp.file "!");
      
temp.lines.savestring(temp.filefalse);
    }
  }
  echo(
"Done! Replaced text a total of" SPC temp.totalLevelChanges SPC "times!");

If a mod would be so kind to update the first post... <3
__________________

"A delayed game is eventually good, but a rushed game is forever bad." - Shigeru Miyamoto
Reply With Quote
  #6  
Old 10-11-2011, 05:15 PM
Emera Emera is offline
Delterian Hybrid
Emera's Avatar
Join Date: Mar 2011
Location: Newcastle Upon-Tyne
Posts: 1,704
Emera is a jewel in the roughEmera is a jewel in the rough
I love you too much...
Attached Thumbnails
Click image for larger version

Name:	xxzirozzrep.PNG
Views:	149
Size:	8.1 KB
ID:	53791  
__________________
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.