Author Topic: Cosmo Canyon save patch  (Read 2940 times)

mav

  • *
  • Posts: 239
  • The Sauce team
    • View Profile
Cosmo Canyon save patch
« on: 2005-05-19 22:47:52 »
I figured that it would be cool to develop patch for Cosmo Canyon so you mustn't see the animation. It would be a simple patch to change plot progression variable (before going to Bugenhaggen it's 0xD5 and after - 0xED). Unfortunetly, I'm not that good in C++ and I'm writing in Delphi, and I have problem with checksum function:

Qhimm's code:

Code: [Select]
int CFF7File::Checksum( char* b )
{
int i = 0, t, d;
long r = 0xFFFF, len = 4336;
long pbit = 0x8000;

while( len-- ) {
t = b[i++];
r ^= t << 8;
for(d=0;d<8;d++) {
if( r & pbit )
r = ( r << 1 ) ^ 0x1021;
else
r <<= 1;
}
r &= ( 1 << 16 ) - 1;
}
return ~r;
}


My code:

Code: [Select]
 var i, t, d, r, len, pbit: integer;
  content: array of char;
    i := 0;
    while (len >= 0) do begin
      len := len - 1;
      i := i + 1;
      t := ord(content[i]);
      r := r xor (t shl 8);
      for d := 0 to 7 do begin
        if(r and pbit)=0 then
          r := ( r shl 1 ) xor $1021
        else
          r := r shl 1;
        r := r and ((1 shl 16) - 1);
      end;
    end;


Could someone point me what I have done wrong ? Program calculates something, but it's diffrent from the original. Help apprieciated :).

Synergy Blades

  • Guest
Cosmo Canyon save patch
« Reply #1 on: 2005-05-19 23:00:33 »
This bit made me wonder:

Code: [Select]
if( r & pbit )
r = ( r << 1 ) ^ 0x1021;


So the r = ( r << 1 ) ^ 0x1021 is run if (r & pbit) is not zero, where as looking at yours you're comparing (r & pbit) to see if it is zero:

Code: [Select]

if(r and pbit)=0 then
r := ( r shl 1 ) xor $1021


Oh and the obvious question since I haven't used Delphi, you have given a value to those variable such as len, p?

mav

  • *
  • Posts: 239
  • The Sauce team
    • View Profile
Cosmo Canyon save patch
« Reply #2 on: 2005-05-19 23:09:32 »
Thanks for posting. Tried this, still, no luck... :-?

Synergy Blades

  • Guest
Cosmo Canyon save patch
« Reply #3 on: 2005-05-19 23:24:14 »
The only other thing I thought about was that Qhimm uses longs for some variables where you use integers. Not sure what range/size/representation Delphi uses for an integer, but if it's different I thought it might be a problem since you're bitshifting r, so if they're of different sizes for example you would end up with a different result.

Anyway, wait til Qhimm arrives (or Ficedula, if you're lucky  :wink: )

Qhimm

  • Founder
  • *
  • Posts: 1996
    • View Profile
    • Qhimm.com
Cosmo Canyon save patch
« Reply #4 on: 2005-05-20 04:31:18 »
Here's what I can see from looking at it (mostly covered by previous posts):
  • God, I wrote ugly code back then.
  • You don't seem to be initializing the variables r, len and pbit.
  • You are pre-incrementing i (before indexing the data) instead of post-incrementing. IIRC Delphi dynamic arrays are zero-based, so this is probably incorrect.
  • You're testing for (r and pbit) = 0, which is the opposite condition of my code. It should be tested for non-zero.
  • I don't think the constriction to 16-bit data size is needed inside the loop, it could probably be moved out to the return statement (ineffective code on my part, not yours).
  • You are not inverting the checksum before returning (i.e. r := r xor $ffff)
  • [/list:u]
    Just guessing that this code would work better:

Code: [Select]
 var i, t, d, r, len, pbit: integer;
  content: array of char;
    i := 0;
    r := $ffff;
    len := 4336;
    while (len >= 0) do begin
      len := len - 1;
      t := ord(content[i]);
      i := i + 1;
      r := r xor (t shl 8);
      for d := 0 to 7 do begin
        if (r and pbit) = 0 then
          r := r shl 1
        else
          r := ( r shl 1 ) xor $1021;
      end;
    end;
    r := (r xor $ffff) and $ffff;

Then again as I'm no Delphi expert... I even had to reverse the if clause since I don't know how to test for value non-zero in Delphi. :P

mav

  • *
  • Posts: 239
  • The Sauce team
    • View Profile
Cosmo Canyon save patch
« Reply #5 on: 2005-05-24 13:22:22 »
I've tried this, still, something is wrong. I guess I'll wait for Fice, because he's good at translating C++ -> Delphi ;)