• 0

Code Replacement Help


Question

I have a rather large Lua script that I have spent days trying to get put together.

I am at the last step and I have 1 final obsticle.

there are 15000+ lines of Lua code and I need at least 4000 lines replaced.

resource.AddFile( "*.lua" )

The * there is an address the thousands of lua files and I need to change that line with

AddCSLuaFile( "*.lua" )

But I cant find any Text editor that will replace text based on variables.

Any suggestions?

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Either use macros if your IDE supports it (for instance Visual Studio), or write a little program in your favorite programming language that opens a text file and checks line by line if it can find the string "resource.AddFile" and replace it with "AddCSLuaFile". Then run it over all your code.

I had to do something similar last summer and I ended up doing everything by hand instead of going through the hassle of writing a program or learning how to use macros. However, I maybe had 200 replacements to make, not 4000. In your case I don't think you have a choice but to automate the process.

EDIT : Can't you just find and replace "resource.AddFile" with "AddCSLuaFile" in any text editor that supports find and replace ?

Link to comment
Share on other sites

  • 0

I cant just replace resource.AddFile with AddCSLuaFile

Because the * is a file addess to many differnt folders and filenames that I cant just copy and rplace.

Heres an example:

resource.AddFile( "addons/counter-strike/lua/weapons/weapon_para/shared.lua" )

resource.AddFile( "addons/counter-strike/lua/weapons/weapon_pumpshotgun/shared.lua" )

resource.AddFile( "addons/counter-strike/lua/weapons/weapon_tmp/shared.lua" )

resource.AddFile( "addons/counter-strike/materials/maps/base.vtf" )

resource.AddFile( "addons/counter-strike/materials/maps/cs_assault.vmt" )

resource.AddFile( "addons/counter-strike/materials/maps/cs_assault.vtf" )

resource.AddFile( "addons/counter-strike/materials/maps/cs_compound.vmt" )

resource.AddFile( "addons/counter-strike/materials/maps/cs_compound.vtf" )

You notice the massivly different addresses and filenames here (Yes this is to the source engine)

I cant copy the address and just do a Ctrl + H reaplce, If I could I wouldn't be asking for help XD

Eveything with .lua" ) at the end need to have AddCSLuaFile(" at the beging instead of resource.AddFile( "

Link to comment
Share on other sites

  • 0

I think this should work. Only tested it on your last post and seemed to work fine. However, make sure you backup your script because I'm not guaranteeing this will work perfectly.

Basically all it does it read in your script, look for lines with .lua and replace the start of them. If .lua is not found, it just passes the line to the output file.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main ()
{
	string str2 (".lua");
	size_t found;
	int number = 0;

	string line;
	ifstream myfile ("input.txt");
	ofstream outfile("out.txt");
	if (myfile.is_open()){
		while (! myfile.eof() )
		{
			getline (myfile,line);

			found = line.find(str2);
			if (found != string::npos){
				number++;

				string replace("AddCSLuaFile");
				line.replace(0, 16, replace);
			}
			outfile << line << endl;
		}
	}

	cout << number << " lines replaced." << endl;

	return 0;
}

Change your ifstream myfile ("input.txt") to your script name and compile. Let me know if you can't compile it.

When I ran it on your post I got this

AddCSLuaFile( "addons/counter-strike/lua/weapons/weapon_para/shared.lua" )
AddCSLuaFile( "addons/counter-strike/lua/weapons/weapon_pumpshotgun/shared.lua" )
AddCSLuaFile( "addons/counter-strike/lua/weapons/weapon_tmp/shared.lua" )
resource.AddFile( "addons/counter-strike/materials/maps/base.vtf" )
resource.AddFile( "addons/counter-strike/materials/maps/cs_assault.vmt" )
resource.AddFile( "addons/counter-strike/materials/maps/cs_assault.vtf" )
resource.AddFile( "addons/counter-strike/materials/maps/cs_compound.vmt" )
resource.AddFile( "addons/counter-strike/materials/maps/cs_compound.vtf" )

Edited by ViZioN
Link to comment
Share on other sites

  • 0

did you solve the problem?

if not this regex should save your day:

resource.AddFile(\(\s*\"[\s\w\\/\--[\p{Zl}]]*\.lua\"\s*\))

Use some editor like Expresso [link] and do a text replace with this string:

AddCSLuaFile$1

The regex would find all lines that have resource.addfile and end with .lua. The replacement string would then take the path extracted by the regex and append it to addcsluafile.

Link to comment
Share on other sites

  • 0
did you solve the problem?

if not this regex should save your day:

resource.AddFile(\(\s*\"[\s\w\\/\--[\p{Zl}]]*\.lua\"\s*\))

Use some editor like Expresso [link] and do a text replace with this string:

AddCSLuaFile$1

The regex would find all lines that have resource.addfile and end with .lua. The replacement string would then take the path extracted by the regex and append it to addcsluafile.

won't dream weaver do a find and replace through out a directory thing?

Link to comment
Share on other sites

  • 0

ViZon, if it isn't too much trouble, could you send me the .exe

Just leave the string as input.txt and output.txt

I appreciate your help with this.

Link to comment
Share on other sites

  • 0
ViZon, if it isn't too much trouble, could you send me the .exe

Just leave the string as input.txt and output.txt

I appreciate your help with this.

Sent you an PM :)

Link to comment
Share on other sites

This topic is now closed to further replies.