Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpqapi cleanup #624

Merged
merged 14 commits into from
Feb 22, 2020
Merged

mpqapi cleanup #624

merged 14 commits into from
Feb 22, 2020

Conversation

glebm
Copy link
Collaborator

@glebm glebm commented Feb 16, 2020

  1. Do not rely on stream positions for getting the initial file size.
  2. Remove most seek calls that were unnecessary.
  3. Replace magic numbers with constants.
  4. A class to manage archive lifetime and all associated data.
  5. Works around save issue on Amiga

@Cowcat5150, could you please test this?

@glebm glebm marked this pull request as ready for review February 16, 2020 04:10
@glebm glebm requested a review from AJenbo February 16, 2020 04:10
@glebm glebm force-pushed the mpqapi-fix1 branch 4 times, most recently from d8f59ec to 45178cf Compare February 16, 2020 11:10
@glebm
Copy link
Collaborator Author

glebm commented Feb 16, 2020

@AJenbo Do the tests run on Windows in CI?

@AJenbo
Copy link
Member

AJenbo commented Feb 16, 2020

@Cowcat5150
Copy link

Cowcat5150 commented Feb 16, 2020

  1. Do not rely on stream positions for getting the initial file size.
  2. Remove most seek calls that were unnecessary.
  3. Replace magic numbers with constants.
  4. A class to manage archive lifetime and all associated data.
  5. Works around save issue on Amiga

@Cowcat5150, could you please test this?

Mmm....latest changes/fixes/ works for debian, but still not saves for Morphos. Opens old single files. Also there are some problems with std::free not being recognized for a Warpos build (for Morphos is ok).

Basically what an earlier mpqapi log frame said;

Opening PROGDIR:single_1.sv
new std::fstream("PROGDIR:single_1.sv", std::ios::binary | std::ios::in | std::ios::out | std::ios::trunc)
seekp(65640, std::ios::beg): failed with "Illegal seek"
Closing PROGDIR:single_1.sv
seekp(0, std::ios::beg): failed with "Illegal seek"
Apps:Diablo>

@glebm
Copy link
Collaborator Author

glebm commented Feb 16, 2020

I guess #ifdef __AMIGA__ doesn't apply to these platforms? Do you know what libc warpos and morphos use?
If it's bebbo/libnix, the illegal seek bug in it might have been fixed just now: bebbo/libnix#30

@Cowcat5150
Copy link

Cowcat5150 commented Feb 16, 2020

I guess #ifdef __AMIGA__ doesn't apply to these platforms? Do you know what stdlib warpos and morphos use?

AMIGA defines are by default included for warpos/morphos:

stdlib.h for warpos (gcc 6.4 - newlib): void free (void *) _NOTHROW;
stdlib.h for morphos (gcc 9); void free __P((void *));

No bebbo/libnix: This is Amiga-like powerpc ecosystem and nothing to do with 68k new gcc's. Thinking about doing some stream tests by my own before going to look out for the Morphos team developers. Warpos can't test it because of the std::free (it uses basically gcc/g++ from Morphos but not the full SDK). Anyways do your own thing.

@glebm

LOL, adding this:

char_buffer[65640];
cur_archive.stream.write(buffer, pBlk->offset);

before

if(!cur_archive_stream.seekp(pBlk->offset, std::ios::beg))
    goto_on_error;

Works ! :)

But then cannot read if I save later:( Got to fix that,,,,,,

Ok. Seems to work now: A simple bool inside InitDefaultMpqHeader to control if a new stream is created and then a check if I had to write this buffer or not.

1. Do not rely on stream positions for getting the initial file size.
2. Remove most `seek` calls that were unnecessary.
3. Replace magic numbers with constants.
4. A class to manage archive lifetime and all associated data.
This fixes fstream logging on Amiga
@glebm
Copy link
Collaborator Author

glebm commented Feb 18, 2020

@Cowcat5150 I've replaced the uses of malloc/free in that file, please try again.

LOL, adding this:

It's exactly what this block does (but without using 60 KiB of stack):

#ifndef __AMIGA__
		// Amiga currently cannot seekp beyond end-of-file, so we fill up the space.
		// The data is incorrect at this point, it will be overwritten on Close.
		// See https://github.com/bebbo/libnix/issues/30
		if (!cur_archive.exists)
			cur_archive.WriteHeaderAndTables();
#endif

May no longer be necessary on Amiga with the latest bebbo/libnix though.

This reduces log noise and reliance on `tellp`.
@Cowcat5150
Copy link

Cowcat5150 commented Feb 18, 2020

@Cowcat5150 I've replaced the uses of malloc/free in that file, please try again.

LOL, adding this:

It's exactly what this block does (but without using 60 KiB of stack):

#ifndef __AMIGA__
		// Amiga currently cannot seekp beyond end-of-file, so we fill up the space.
		// The data is incorrect at this point, it will be overwritten on Close.
		// See https://github.com/bebbo/libnix/issues/30
		if (!cur_archive.exists)
			cur_archive.WriteHeaderAndTables();
#endif

May no longer be necessary on Amiga with the latest bebbo/libnix though.

Previously I enabled it for use with Amiga but got same problems. Will try your new changes today.

Tested. New delete compiles but still the save problem: Using my buffer hack kinda works but now saving sets the system in bad situation that leads to crash sooner or later.

Forget to mention: The use of truncate in file_util.h/ResizeFile function can be changed by ftruncate or is totally different ?. "truncate" not available in Warpos and I use "ftruncate"......;(

@qndel
Copy link
Member

qndel commented Feb 18, 2020

Crashes for me when trying to create a character in x64 release/x64 debug builds in visual studio on windows

@glebm
Copy link
Collaborator Author

glebm commented Feb 18, 2020

@qndel Log and stack trace please. I don't have a Windows with Visual Studio at the moment.

Tracked down and fixed the Windows crash. together with @AJenbo. Release vs Debug issue still not fixed (isn't affected by this PR)

@glebm glebm force-pushed the mpqapi-fix1 branch 3 times, most recently from b6bbc5d to fc4368e Compare February 19, 2020 00:39
@glebm
Copy link
Collaborator Author

glebm commented Feb 19, 2020

@Cowcat5150 OK, I've added some code to ensure that we do not seekp beyond EOF in mpqapi_write_file_contents on __AMIGA__. This should hopefully work now.

Regarding ftruncate, it's fine to use it instead on platforms where truncate is not available. Please send a PR!

@AJenbo
Copy link
Member

AJenbo commented Feb 19, 2020

<3 SourceT/file_util_test.cpp

@Cowcat5150
Copy link

Cowcat5150 commented Feb 19, 2020

Seems to work as expected for Morphos, hope is good for rest of Amiga's . :)

On a previous, previous mpqapi fix, due to a buggy/old MOS sdl library (I think) I ended with a single_0.sv file impossible to open even on debian. Supposedly files are "safe" against problems when game fails to continue but "MPQ free list error" was the message.

Hope that this is also fixed (was source from a couple of days ago). Good work anyways.

@AJenbo AJenbo merged commit c9c1c32 into diasurgical:master Feb 22, 2020
@qndel
Copy link
Member

qndel commented Feb 22, 2020

Can't create new characters, crashes while saving on windows
image
crash while making a new char
@glebm

@glebm
Copy link
Collaborator Author

glebm commented Feb 22, 2020

@qndel Is this from the latest version? I thought aec52ae fixed it

@qndel
Copy link
Member

qndel commented Feb 22, 2020

well I'm not a git god, but I pulled changes from master to my light branch then even tested with just the master 🤷‍♂

@glebm glebm deleted the mpqapi-fix1 branch February 22, 2020 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants