"Failed to read info.json" on uploaded, yet functional mod

A place to talk about the official Factorio mod portal (https://mods.factorio.com)
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 583
Joined: Tue May 19, 2015 1:56 am
Contact:

"Failed to read info.json" on uploaded, yet functional mod

Post by Reika »

I sometimes get an issue when uploading a mod to the portal, telling me that it could not read the info.json file. In particular, I have noticed the following:
  • *How the mod zip is compiled matters; ones compiled with WinRAR have the greatest rate of success
    *Editing the zip in an attempt to 'fix' the json usually ends up with a zip with two info.json files. Unexpectedly, these files usually work both on the portal and in game.
    *Opening the zip and closing it again can sometimes make it work, more frequently if some no-net-change modification is made (like opening a file within, editing it, saving it, reverting the change, and saving again).
In particular it seems to have a problem with auto-compiled zip files, even though the game itself loads them without issue.

Can someone please explain how the portal's parser works so I can get this working more reliably?

EDIT:

Example:
Image

For this zip;

For this info.json, which works in game, and has worked before (including with the 0.0.6 version):

Code: Select all

{
  "name": "FTweaks",
  "version": "0.0.6",
  "title": "FTweaks",
  "author": "Reika",
  "contact": "",
  "homepage": "",
  "factorio_version": "0.15",
  "description": "Tweaks the game to either fix minor issues, add QoL features, or make larger changes to game behavior.",
  "dependencies": ["base >= 0.15.0"]
}
EDIT 2:
I opened the jar, opened the json, added a "-" after the '}' and then backspaced, re-saved the file, got two jsons in the zip, and it uploaded fine.
Image
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 583
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by Reika »

Well, I learned something: The actual code used to compile the zips matters too, and seems to be the deciding factor. In particular, however WinRAR does it always works. Editing the contents of a jar/zip actually repack the whole thing using WinRAR's encoding, which the portal can accept.

My custom compiler, well...

This code does not work:

Code: Select all

public static void zipFolder(File source, File zipPath) throws IOException {
		final Path sourceFolderPath = source.toPath();
		final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipPath));
		Files.walkFileTree(sourceFolderPath, new SimpleFileVisitor<Path>() {
			@Override
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
				zos.putNextEntry(new ZipEntry(sourceFolderPath.relativize(file).toString()));
				Files.copy(file, zos);
				zos.closeEntry();
				return FileVisitResult.CONTINUE;
			}
		});
		zos.close();
		}
Nor does this:

Code: Select all

public class ZipHelper { // Not parsable by factorio mod portal

	private final String input;
	private final String output;

	private final List<String> fileList = new ArrayList<String>();

	private ZipHelper(String in, String out) {
		input = in;
		output = out;
	}

	public static void zipFolder(String in, String out) {
		ZipHelper appZip = new ZipHelper(in, out);
		appZip.readInput(new File(in));
		appZip.makeZIP();
	}

	private void makeZIP() {
		byte[] buffer = new byte[1024];
		String source = new File(input).getName();
		FileOutputStream fos = null;
		ZipOutputStream zos = null;
		try {
			fos = new FileOutputStream(output);
			zos = new ZipOutputStream(fos);

			FileInputStream in = null;

			for (String file : fileList) {
				ZipEntry ze = new ZipEntry(source + File.separator + file);
				zos.putNextEntry(ze);
				try {
					in = new FileInputStream(input + File.separator + file);
					int len;
					while ((len = in.read(buffer)) > 0) {
						zos.write(buffer, 0, len);
					}
				}
				finally {
					in.close();
				}
			}

			zos.closeEntry();
		}
		catch (IOException ex) {
			ex.printStackTrace();
		}
		finally {
			try {
				zos.close();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void readInput(File node) {
		// add file only
		if (node.isFile()) {
			fileList.add(this.generateZipEntry(node.toString()));
		}

		if (node.isDirectory()) {
			String[] subNote = node.list();
			for (String filename : subNote) {
				this.readInput(new File(node, filename));
			}
		}
	}

	private String generateZipEntry(String file) {
		return file.substring(input.length() + 1, file.length());
	}
}
But this does:

Code: Select all

	public static void zipFolder(String srcFolder, String destZipFile) throws IOException {
		ZipOutputStream zip = null;
		FileOutputStream fileWriter = null;

		fileWriter = new FileOutputStream(destZipFile);
		zip = new ZipOutputStream(fileWriter);

		addFolderToZip("", srcFolder, zip);
		zip.flush();
		zip.close();
	}

	private static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

		File folder = new File(srcFile);
		if (folder.isDirectory()) {
			addFolderToZip(path, srcFile, zip);
		}
		else {
			byte[] buf = new byte[1024];
			int len;
			FileInputStream in = new FileInputStream(srcFile);
			zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
			while ((len = in.read(buf)) > 0) {
				zip.write(buf, 0, len);
			}
			in.close();
		}
	}

	private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws IOException {
		File folder = new File(srcFolder);

		for (String fileName : folder.list()) {
			if (path.equals("")) {
				addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
			}
			else {
				addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
			}
		}
	}
In theory they are all supposed to be the same. Why am I getting different results? Is there something weird on the server's end about filesystem encoding or something?
Image
User avatar
HanziQ
Former Staff
Former Staff
Posts: 630
Joined: Fri Mar 27, 2015 7:07 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by HanziQ »

The zip is read using https://docs.python.org/3.5/library/zipfile.html . If you provide examples of a broken zip, I can take a look.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 583
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by Reika »

HanziQ wrote:The zip is read using https://docs.python.org/3.5/library/zipfile.html . If you provide examples of a broken zip, I can take a look.
Here are a whole bunch - my entire set of mods. All of them were built just now using method 2 (the second non-working one) in the above post (so selected because it procedurally most resembles the working one); I tested and made sure that they were - as expected - rejected by the portal.

http://www.mediafire.com/file/s0utwwk0y ... n+zips.zip

EDIT:
Also, if it helps:
viewtopic.php?f=135&t=51035
Image
User avatar
HanziQ
Former Staff
Former Staff
Posts: 630
Joined: Fri Mar 27, 2015 7:07 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by HanziQ »

I see, it is actually not read by zipfile, but with a different method.

The problem is path separators, the ZIP standard requires the use of forward slashes, you are using backshlashes.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 583
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by Reika »

HanziQ wrote:I see, it is actually not read by zipfile, but with a different method.

The problem is path separators, the ZIP standard requires the use of forward slashes, you are using backshlashes.
I call "File.separator", not explicitly calling any one slash type, but I suspect you mean that when run on a Windows machine "File.separator" resolves to "\", which then makes a ZIP where those slash types persist. So, amusingly, the fix for the second solution is to hardcode the "/", and NOT use the "coding standard" of File.separator.

One more case of 'best practice' going straight into the toilet. :P
Image
User avatar
HanziQ
Former Staff
Former Staff
Posts: 630
Joined: Fri Mar 27, 2015 7:07 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by HanziQ »

Well I find a standard using just 1 type of slashes better, than having to use a variable to determine that.
torne
Filter Inserter
Filter Inserter
Posts: 343
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by torne »

It's explicitly written in the specification defining the ZIP format that you must always use a literal "'/" for path separators. File.separator is only appropriate when dealing with the current OS's filesystem APIs.

Best practise is to obey the standard for the format you are using :)
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 583
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by Reika »

torne wrote:It's explicitly written in the specification defining the ZIP format that you must always use a literal "'/" for path separators. File.separator is only appropriate when dealing with the current OS's filesystem APIs.
Tell that to the 50+ people (read: basically any time I have ever seen it mentioned) on StackOverflow who swear that slashes of any kind should never be explicitly used and that coding one is worthy of derision comparable to doing this.
Image
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3729
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by DaveMcW »

Reika wrote:slashes of any kind should never be explicitly used
... when using an operating system API.

Code: Select all

public final class Constants {
    public static final char ZIP_SEPARATOR = '/';
}
// Do you feel better now?
torne
Filter Inserter
Filter Inserter
Posts: 343
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by torne »

Reika wrote:Tell that to the 50+ people (read: basically any time I have ever seen it mentioned) on StackOverflow who swear that slashes of any kind should never be explicitly used and that coding one is worthy of derision comparable to doing this.
If I happened to come across that on StackOverflow I would indeed tell them that they are wrong, because they are. As has been emphasised multiple times: this only applies to operating system filesystem APIs. When you're dealing with a protocol, file format, or library that explicitly documents that it uses a particular character for a particular purpose then you must do what its documentation says - you can tell the author of that spec/library/whatever that you think they're wrong and they should change it, if you like (though it's several decades too late to do this for the ZIP format), but you can't just decide you're going to do something else unilaterally and expect it to work. :)
MarcGamesons
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sat Aug 22, 2015 4:14 pm
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by MarcGamesons »

I am sorry if I should have opened my own thread, but I am getting this error message too.

I am using the windows 10 built in zip function to make the archive. This is how my info.json is structured https://github.com/MarcGamesons/factori ... /info.json
User avatar
HanziQ
Former Staff
Former Staff
Posts: 630
Joined: Fri Mar 27, 2015 7:07 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by HanziQ »

Please upload the mod zip file
MarcGamesons
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sat Aug 22, 2015 4:14 pm
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by MarcGamesons »

You can download the mods zip file here https://github.com/MarcGamesons/factori ... _1.2.0.zip
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3729
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by DaveMcW »

You need to make an unzipped folder named: cheaper_landfill_1.2.0

Then use Windows zip function to zip the entire folder.
MarcGamesons
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sat Aug 22, 2015 4:14 pm
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by MarcGamesons »

I am really sorry I packaged it wrong, it happens every other time I update my mod and I spend hours until I notice it every time. :(

But thanks to everyone that took their time trying to help me, I still can't believe that I did it wrong again... I wrote myself a note so I don't do it wrong the next time :)
d3x0r
Filter Inserter
Filter Inserter
Posts: 316
Joined: Sun Jun 04, 2017 8:56 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by d3x0r »

I realize I'm more than a week late but...

Arguing that 'I'm using a windows path separator which only windows understands' is a correct thing to do is horrible wrong when making a cross platform package that factorio can use on linux and mac systems which wouldn't accept backslash as a valid character.

Windows has no problem (internally) using either forward or back slashes; it's only poorly written command line tools and cmd.exe builtin functions that puke when being passed the wrong slash.

Stack overflow encouraging anything other than a universal character that can be used on any file system is a horrible thing for them to do too.
User avatar
fishycat
Filter Inserter
Filter Inserter
Posts: 315
Joined: Thu Apr 09, 2015 7:38 pm
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by fishycat »

I came across this problem, too. I updated a mod for 0.16 and tried to upload it to the mod-portal, but it gave me the info.json error.

Next I looked here and found this thread, oh it is because I use 7-zip maybe. Then I got WinRAR, created a new folder and then zipped it with WinRAR, but again info.json error from mod-portal.

What am I overseeing?
Attachments
AlienSpaceScience_0.0.4.zip
(27.21 KiB) Downloaded 115 times
User avatar
HanziQ
Former Staff
Former Staff
Posts: 630
Joined: Fri Mar 27, 2015 7:07 am
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by HanziQ »

fishycat wrote:I came across this problem, too. I updated a mod for 0.16 and tried to upload it to the mod-portal, but it gave me the info.json error.

Next I looked here and found this thread, oh it is because I use 7-zip maybe. Then I got WinRAR, created a new folder and then zipped it with WinRAR, but again info.json error from mod-portal.

What am I overseeing?
Your info.json is missing a '}' at the end.
User avatar
fishycat
Filter Inserter
Filter Inserter
Posts: 315
Joined: Thu Apr 09, 2015 7:38 pm
Contact:

Re: "Failed to read info.json" on uploaded, yet functional mod

Post by fishycat »

Sorry, maybe I'm a little dumb here, but my info.json is like all the others
info.json
There is a } at the end. But for safety I tested it with the next one, and that also failed.
info.json
Post Reply

Return to “Mod portal Discussion”