paavohtl Claude Opus 4.6 (1M context) commited on
Commit
2716775
·
1 Parent(s): 5d6bf83

refactor: use c"..." CStr literals instead of b"...\0" binary strings

Browse files

Replace manual null-terminated byte strings with Rust's C string
literals for compile-time null termination guarantee:
- b"something\0".as_ptr() → c"something".as_ptr().cast()
- b"something\0" in arrays → c"something".to_bytes_with_nul()

Kept b"..." for two strings mutated at runtime (gfx_c_path, name_buf).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

crates/openwa-core/src/display/gradient.rs CHANGED
@@ -114,7 +114,7 @@ pub(crate) unsafe fn compute_complex_gradient(
114
 
115
  // Step 1: Load gradient.img
116
  let gradient_sprite =
117
- call_gfx_find_and_load(land_layer, b"gradient.img\0".as_ptr(), layer3_ctx);
118
  if gradient_sprite.is_null() {
119
  return;
120
  }
@@ -143,7 +143,8 @@ pub(crate) unsafe fn compute_complex_gradient(
143
 
144
  // Step 3: If heights match, also set gradient_image_2
145
  if target_rows == gradient_height {
146
- let gradient2 = call_gfx_find_and_load(land_layer, b"gradient.img\0".as_ptr(), layer3_ctx);
 
147
  (*ddgame).gradient_image_2 = gradient2;
148
  }
149
 
 
114
 
115
  // Step 1: Load gradient.img
116
  let gradient_sprite =
117
+ call_gfx_find_and_load(land_layer, c"gradient.img".as_ptr().cast(), layer3_ctx);
118
  if gradient_sprite.is_null() {
119
  return;
120
  }
 
143
 
144
  // Step 3: If heights match, also set gradient_image_2
145
  if target_rows == gradient_height {
146
+ let gradient2 =
147
+ call_gfx_find_and_load(land_layer, c"gradient.img".as_ptr().cast(), layer3_ctx);
148
  (*ddgame).gradient_image_2 = gradient2;
149
  }
150
 
crates/openwa-core/src/engine/ddgame.rs CHANGED
@@ -755,34 +755,34 @@ unsafe fn init_graphics_and_resources(
755
  let paths: [&[u8]; 3] = if headless != 0 {
756
  if display_flags == 0 {
757
  [
758
- b"data\\Gfx\\Gfx.dir\0",
759
- b"data\\Gfx\\Gfx0.dir\0",
760
- b"data\\Gfx\\Gfx1.dir\0",
761
  ]
762
  } else {
763
  [
764
- b"data\\Gfx\\Gfx.dir\0",
765
- b"data\\Gfx\\Gfx1.dir\0",
766
- b"data\\Gfx\\Gfx0.dir\0",
767
  ]
768
  }
769
  } else if display_flags == 0 {
770
  [
771
- b"data\\Gfx\\Gfx0.dir\0",
772
- b"data\\Gfx\\Gfx1.dir\0",
773
- b"data\\Gfx\\Gfx.dir\0",
774
  ]
775
  } else {
776
  [
777
- b"data\\Gfx\\Gfx1.dir\0",
778
- b"data\\Gfx\\Gfx0.dir\0",
779
- b"data\\Gfx\\Gfx.dir\0",
780
  ]
781
  };
782
 
783
  let mut gfx_loaded_idx = 0u32;
784
  for (i, path) in paths.iter().enumerate() {
785
- let fp = fopen(path.as_ptr(), b"rb\0".as_ptr());
786
  (*gfx1).file_handle = fp;
787
  if !fp.is_null()
788
  && call_gfx_load_dir(gfx1 as *mut u8, crate::render::gfx_dir::gfx_load_dir_addr()) != 0
@@ -813,11 +813,11 @@ unsafe fn init_graphics_and_resources(
813
  let gfx2 = GfxDir::alloc(gfx_dir_vtable);
814
  (*wrapper).secondary_gfx_dir = gfx2 as *mut u8;
815
 
816
- let fp = fopen(gfx_c_path.as_ptr(), b"rb\0".as_ptr());
817
  (*gfx2).file_handle = fp;
818
  let load_addr = crate::render::gfx_dir::gfx_load_dir_addr();
819
  if fp.is_null() || call_gfx_load_dir(gfx2 as *mut u8, load_addr) == 0 {
820
- let fp2 = fopen(b"data\\Gfx\\Gfx.dir\0".as_ptr(), b"rb\0".as_ptr());
821
  (*gfx2).file_handle = fp2;
822
  if fp2.is_null() || call_gfx_load_dir(gfx2 as *mut u8, load_addr) == 0 {
823
  panic!("DDGame: couldn't open secondary Gfx.dir");
@@ -1237,8 +1237,14 @@ unsafe fn init_graphics_and_resources(
1237
  DDDisplay::set_active_layer(disp, 3);
1238
 
1239
  if (*wrapper).gfx_mode != 0 {
1240
- DDDisplay::load_sprite_by_layer(disp, 3, 0x26D, land_layer, b"back.spr\0".as_ptr());
1241
- DDDisplay::load_sprite(disp, 3, 0x26E, 0, land_layer, b"debris.spr\0".as_ptr());
 
 
 
 
 
 
1242
  }
1243
 
1244
  DDDisplay::load_sprite_by_layer(
@@ -1246,7 +1252,7 @@ unsafe fn init_graphics_and_resources(
1246
  2,
1247
  0x26C,
1248
  water_layer,
1249
- b"layer\\layer.spr|layer.spr\0".as_ptr(),
1250
  );
1251
 
1252
  (*ddgame).gradient_image_2 = core::ptr::null_mut();
@@ -1259,7 +1265,7 @@ unsafe fn init_graphics_and_resources(
1259
  if s_var1 < 0x61 && level_height == 0x2B8 {
1260
  // Simple gradient: load gradient.img directly
1261
  let gradient =
1262
- call_gfx_find_and_load(land_layer, b"gradient.img\0".as_ptr(), layer3_ctx);
1263
  (*ddgame).gradient_image = gradient;
1264
  } else {
1265
  compute_complex_gradient(ddgame, land_layer, layer3_ctx, s_var1);
@@ -1271,7 +1277,7 @@ unsafe fn init_graphics_and_resources(
1271
  // In the original, fill.img uses piStack_126c which the decompiler
1272
  // shows was set from piVar3 (water_layer from landscape+0xB38).
1273
  let fill_sprite =
1274
- call_gfx_find_and_load(water_layer, b"fill.img\0".as_ptr(), layer2_ctx);
1275
  if !fill_sprite.is_null() {
1276
  // Get pixel value: fill_sprite->vtable[4](0, 0)
1277
  let fill_vt = *(fill_sprite as *const *const u32);
 
755
  let paths: [&[u8]; 3] = if headless != 0 {
756
  if display_flags == 0 {
757
  [
758
+ c"data\\Gfx\\Gfx.dir".to_bytes_with_nul(),
759
+ c"data\\Gfx\\Gfx0.dir".to_bytes_with_nul(),
760
+ c"data\\Gfx\\Gfx1.dir".to_bytes_with_nul(),
761
  ]
762
  } else {
763
  [
764
+ c"data\\Gfx\\Gfx.dir".to_bytes_with_nul(),
765
+ c"data\\Gfx\\Gfx1.dir".to_bytes_with_nul(),
766
+ c"data\\Gfx\\Gfx0.dir".to_bytes_with_nul(),
767
  ]
768
  }
769
  } else if display_flags == 0 {
770
  [
771
+ c"data\\Gfx\\Gfx0.dir".to_bytes_with_nul(),
772
+ c"data\\Gfx\\Gfx1.dir".to_bytes_with_nul(),
773
+ c"data\\Gfx\\Gfx.dir".to_bytes_with_nul(),
774
  ]
775
  } else {
776
  [
777
+ c"data\\Gfx\\Gfx1.dir".to_bytes_with_nul(),
778
+ c"data\\Gfx\\Gfx0.dir".to_bytes_with_nul(),
779
+ c"data\\Gfx\\Gfx.dir".to_bytes_with_nul(),
780
  ]
781
  };
782
 
783
  let mut gfx_loaded_idx = 0u32;
784
  for (i, path) in paths.iter().enumerate() {
785
+ let fp = fopen(path.as_ptr(), c"rb".as_ptr().cast());
786
  (*gfx1).file_handle = fp;
787
  if !fp.is_null()
788
  && call_gfx_load_dir(gfx1 as *mut u8, crate::render::gfx_dir::gfx_load_dir_addr()) != 0
 
813
  let gfx2 = GfxDir::alloc(gfx_dir_vtable);
814
  (*wrapper).secondary_gfx_dir = gfx2 as *mut u8;
815
 
816
+ let fp = fopen(gfx_c_path.as_ptr(), c"rb".as_ptr().cast());
817
  (*gfx2).file_handle = fp;
818
  let load_addr = crate::render::gfx_dir::gfx_load_dir_addr();
819
  if fp.is_null() || call_gfx_load_dir(gfx2 as *mut u8, load_addr) == 0 {
820
+ let fp2 = fopen(c"data\\Gfx\\Gfx.dir".as_ptr().cast(), c"rb".as_ptr().cast());
821
  (*gfx2).file_handle = fp2;
822
  if fp2.is_null() || call_gfx_load_dir(gfx2 as *mut u8, load_addr) == 0 {
823
  panic!("DDGame: couldn't open secondary Gfx.dir");
 
1237
  DDDisplay::set_active_layer(disp, 3);
1238
 
1239
  if (*wrapper).gfx_mode != 0 {
1240
+ DDDisplay::load_sprite_by_layer(
1241
+ disp,
1242
+ 3,
1243
+ 0x26D,
1244
+ land_layer,
1245
+ c"back.spr".as_ptr().cast(),
1246
+ );
1247
+ DDDisplay::load_sprite(disp, 3, 0x26E, 0, land_layer, c"debris.spr".as_ptr().cast());
1248
  }
1249
 
1250
  DDDisplay::load_sprite_by_layer(
 
1252
  2,
1253
  0x26C,
1254
  water_layer,
1255
+ c"layer\\layer.spr|layer.spr".as_ptr().cast(),
1256
  );
1257
 
1258
  (*ddgame).gradient_image_2 = core::ptr::null_mut();
 
1265
  if s_var1 < 0x61 && level_height == 0x2B8 {
1266
  // Simple gradient: load gradient.img directly
1267
  let gradient =
1268
+ call_gfx_find_and_load(land_layer, c"gradient.img".as_ptr().cast(), layer3_ctx);
1269
  (*ddgame).gradient_image = gradient;
1270
  } else {
1271
  compute_complex_gradient(ddgame, land_layer, layer3_ctx, s_var1);
 
1277
  // In the original, fill.img uses piStack_126c which the decompiler
1278
  // shows was set from piVar3 (water_layer from landscape+0xB38).
1279
  let fill_sprite =
1280
+ call_gfx_find_and_load(water_layer, c"fill.img".as_ptr().cast(), layer2_ctx);
1281
  if !fill_sprite.is_null() {
1282
  // Get pixel value: fill_sprite->vtable[4](0, 0)
1283
  let fill_vt = *(fill_sprite as *const *const u32);