Skip to content

Commit

Permalink
Generate CON_* constants when loading System.ocg
Browse files Browse the repository at this point in the history
As the PlayerControls.txt file is in System.ocg and doesn't require
recompiling OC, changing it shouldn't require recompiling ocmapgen
either.
  • Loading branch information
lluchs committed Apr 3, 2017
1 parent a09eef7 commit b540df1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 108 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build = "build.rs"
[dependencies]
image = "0.12"
error-chain = "0.10"
regex = "0.2"

[build-dependencies]
gcc = "0.3"
Expand Down
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ src/ffi.rs: src/cpp-handles/*.h

exclude_fns := GetStartupPlayerCount|GetStartupTeamCount
script_sources := openclonk/src/game/C4GameScript.cpp openclonk/src/object/C4ObjectScript.cpp
player_controls := openclonk/planet/System.ocg/PlayerControls.txt
src/StandaloneCompat.c: $(script_sources) $(player_controls)
src/StandaloneCompat.c: $(script_sources)
echo "/* Automatically generated from: $(script_sources) */" > $@
sed -En '/$(exclude_fns)/!s/^[a-zA-Z0-9_ <>*]*Fn(\w+)\(.*$$/global func \1(...) { FatalError("standalone stub"); }/p' $(script_sources) >> $@
sed -En 's/^\s*\{\s*"(\w+)"\s*,\s*C4V_Int.*/static const \1 = 0;/p' $(script_sources) >> $@
echo >> $@
echo "/* Automatically generated from: $(player_controls) */" >> $@
sed -En 's/\s*Identifier=(\w+).*/static const CON_\1 = 0;/p' $(player_controls) >> $@

.PHONY: all
101 changes: 0 additions & 101 deletions src/StandaloneCompat.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,104 +618,3 @@ static const ANIM_Remove = 0;
static const AM_None = 0;
static const AM_DrawBefore = 0;
static const AM_MatchSkeleton = 0;

/* Automatically generated from: openclonk/planet/System.ocg/PlayerControls.txt */
static const CON_Aim = 0;
static const CON_GUICursor = 0;
static const CON_AimAxisUp = 0;
static const CON_AimAxisDown = 0;
static const CON_AimAxisLeft = 0;
static const CON_AimAxisRight = 0;
static const CON_Left = 0;
static const CON_Right = 0;
static const CON_Up = 0;
static const CON_Down = 0;
static const CON_Jump = 0;
static const CON_FallThrough = 0;
static const CON_Roll = 0;
static const CON_ModifierMenu1 = 0;
static const CON_QuickSwitch = 0;
static const CON_InventoryShiftForward = 0;
static const CON_InventoryShiftBackward = 0;
static const CON_Throw = 0;
static const CON_Drop = 0;
static const CON_PickUp = 0;
static const CON_PickUpNext_Left = 0;
static const CON_PickUpNext_Right = 0;
static const CON_PickUpNext_Stop = 0;
static const CON_PickUpNext_All = 0;
static const CON_Hotkey1 = 0;
static const CON_Hotkey2 = 0;
static const CON_Hotkey3 = 0;
static const CON_Hotkey4 = 0;
static const CON_Hotkey5 = 0;
static const CON_Hotkey6 = 0;
static const CON_Hotkey7 = 0;
static const CON_Hotkey8 = 0;
static const CON_Hotkey9 = 0;
static const CON_Hotkey0 = 0;
static const CON_DropHotkey1 = 0;
static const CON_DropHotkey2 = 0;
static const CON_DropHotkey3 = 0;
static const CON_DropHotkey4 = 0;
static const CON_DropHotkey5 = 0;
static const CON_DropHotkey6 = 0;
static const CON_DropHotkey7 = 0;
static const CON_DropHotkey8 = 0;
static const CON_DropHotkey9 = 0;
static const CON_DropHotkey0 = 0;
static const CON_Use = 0;
static const CON_UseAlt = 0;
static const CON_CancelUse = 0;
static const CON_CancelMenu = 0;
static const CON_Contents = 0;
static const CON_Interact = 0;
static const CON_InteractNext_Left = 0;
static const CON_InteractNext_Right = 0;
static const CON_InteractNext_Stop = 0;
static const CON_InteractNext_CycleObject = 0;
static const CON_InteractionHotkey1 = 0;
static const CON_InteractionHotkey2 = 0;
static const CON_InteractionHotkey3 = 0;
static const CON_InteractionHotkey4 = 0;
static const CON_InteractionHotkey5 = 0;
static const CON_InteractionHotkey6 = 0;
static const CON_InteractionHotkey7 = 0;
static const CON_InteractionHotkey8 = 0;
static const CON_InteractionHotkey9 = 0;
static const CON_InteractionHotkey0 = 0;
static const CON_NextCrew = 0;
static const CON_PreviousCrew = 0;
static const CON_PlayerHotkey1 = 0;
static const CON_PlayerHotkey2 = 0;
static const CON_PlayerHotkey3 = 0;
static const CON_PlayerHotkey4 = 0;
static const CON_PlayerHotkey5 = 0;
static const CON_PlayerHotkey6 = 0;
static const CON_PlayerHotkey7 = 0;
static const CON_PlayerHotkey8 = 0;
static const CON_PlayerHotkey9 = 0;
static const CON_PlayerHotkey0 = 0;
static const CON_GUIClick1 = 0;
static const CON_GUIClick2 = 0;
static const CON_MenuOK = 0;
static const CON_MenuCancel = 0;
static const CON_MenuLeft = 0;
static const CON_MenuRight = 0;
static const CON_MenuUp = 0;
static const CON_MenuDown = 0;
static const CON_ObjectMenuTextComplete = 0;
static const CON_ObjectMenuOK = 0;
static const CON_ObjectMenuOKAll = 0;
static const CON_ObjectMenuSelect = 0;
static const CON_ObjectMenuCancel = 0;
static const CON_ObjectMenuLeft = 0;
static const CON_ObjectMenuRight = 0;
static const CON_ObjectMenuUp = 0;
static const CON_ObjectMenuDown = 0;
static const CON_CursorPos = 0;
static const CON_PlayerMenu = 0;
static const CON_ZoomIn = 0;
static const CON_ZoomOut = 0;
static const CON_WheelZoomIn = 0;
static const CON_WheelZoomOut = 0;
21 changes: 19 additions & 2 deletions src/easy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::io;
use std::fs::File;
use std::path::Path;

use regex::bytes::Regex;

/// Provides an easy-to-use API for rendering mape maps.
pub struct Easy {
pub material_map: MaterialMap,
Expand Down Expand Up @@ -58,8 +60,7 @@ impl Easy {
let root_group = Group::open(path.to_str().unwrap(), false)?;
self.mapgen.set_root_group(&root_group)?;
// Load the top-level System.ocg
let system_group = Group::open(system_ocg.to_str().unwrap(), false)?;
self.mapgen.load_system(&system_group)?;
self.load_system(&system_ocg)?;
break;
}

Expand Down Expand Up @@ -94,6 +95,22 @@ impl Easy {
Ok(())
}

fn load_system(&mut self, path: &Path) -> Result<()> {
let system_group = Group::open(path.to_str().unwrap(), false)?;
// Try to load player controls to generate CON_ constants.
if let Ok(data) = system_group.load_entry("PlayerControls.txt") {
let re = Regex::new(r"(?m)^\s*Identifier=(\w+)").unwrap();
let mut script = String::new();
for cap in re.captures_iter(&data) {
// The regex will only match UTF-8, so from_utf8_lossy won't have to convert
// anything. I don't think OC will accept special characters there anyways.
script.push_str(&format!("static const CON_{} = 0;\n", String::from_utf8_lossy(&cap[1])));
}
self.mapgen.load_script("PlayerControlsCompat.c", &script)?;
}
self.mapgen.load_system(&system_group)
}

/// Entry point for rendering the map.
pub fn build(&self) -> RenderConfig {
RenderConfig {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate image;
#[macro_use]
extern crate error_chain;
extern crate regex;

mod ffi;
mod group;
Expand Down

0 comments on commit b540df1

Please sign in to comment.