Skip to content

Commit

Permalink
#593 Don't check in rc and rc_mac_dlg files anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed Jun 11, 2022
1 parent b2c2823 commit 6a3c65d
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 604 deletions.
8 changes: 4 additions & 4 deletions CONTRIBUTING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ cargo fmt

== GUI

The GUI dialogs are defined in the `dialogs` directory. Whenever ReaLearn is built, the code there generates an old-school Windows dialog resource file in link:main/src/infrastructure/ui/msvc/msvc.rc[msvc.rc], a Rust file which contains all the resource ID constants and a corresponding C header file (in case you want to preview the dialog in a visual resource editor).
The GUI dialogs are defined in the `dialogs` directory. Whenever ReaLearn is built, the code there generates an old-school Windows dialog resource file (`target/generated/msvc.rc`) and a Rust file which contains all the resource ID constants (`main/src/infrastructure/ui/bindings.rs`).

Previously I used the Visual C++ 2019 resource editor to WYSIWYG-edit this file as part of the solution
Previously I used the Visual Studio C++ 2019 resource editor to WYSIWYG-edit this file as part of the solution
link:main/src/infrastructure/ui/msvc/msvc.sln[msvc.sln], but this was too tedious.

WARNING: Don't edit the RC file, the changes will be overwritten at build time! Adjust the Rust code in the `dialogs` directory instead.
WARNING: You can still preview the generated file in Visual Studio but don't edit the RC file, the changes will be overwritten at build time! Adjust the Rust code in the `dialogs` directory instead.

On macOS and Linux, an extra step will happen at build time: It will try to use a PHP script (part of Cockos SWELL) to generate
`main/src/infrastructure/ui/msvc/msvc.rc_mac_dlg`, which is a translation of the RC file to C code using SWELL. So make sure you have PHP installed on these platforms!
`target/generated/msvc.rc_mac_dlg`, which is a translation of the RC file to C code using SWELL. So make sure you have PHP installed on these platforms!

== Test

Expand Down
10 changes: 5 additions & 5 deletions dialogs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mod message_panel;
mod shared_group_mapping_panel;
mod yaml_editor_panel;

pub fn generate_dialog_files(out_dir: impl AsRef<Path>) {
pub fn generate_dialog_files(rc_dir: impl AsRef<Path>, bindings_file: impl AsRef<Path>) {
let default_font = Font {
name: "Ms Shell Dlg",
size: 8,
Expand Down Expand Up @@ -76,21 +76,21 @@ pub fn generate_dialog_files(out_dir: impl AsRef<Path>) {
let header_info = resource.generate_info(&context);
// Write C header file (in case we want to use a resource editor to preview the dialogs)
let c_header_code = ResourceInfoAsCHeaderCode(&header_info).to_string();
std::fs::write(out_dir.as_ref().join("msvc/resource.h"), c_header_code)
std::fs::write(rc_dir.as_ref().join("resource.h"), c_header_code)
.expect("couldn't write C header file");
// Write Rust file (so we don't have to do it via bindgen, which is slow)
let rust_code = ResourceInfoAsRustCode(&header_info).to_string();
std::fs::write(out_dir.as_ref().join("bindings.rs"), rust_code)
.expect("couldn't write Rust bindings file");
std::fs::write(bindings_file, rust_code).expect("couldn't write Rust bindings file");
// Write rc file
let rc_file_header = include_str!("rc_file_header.txt");
let rc_file_footer = include_str!("rc_file_footer.txt");
let rc_file_content = format!("{}\n\n{}\n\n{}", rc_file_header, resource, rc_file_footer);
let mut output = Vec::new();
// Write UTF_16LE BOM
output.write_all(&[0xFF, 0xFE]).unwrap();
// Write UTF_16LE contents
for utf16 in rc_file_content.encode_utf16() {
output.write_all(&utf16.to_le_bytes()).unwrap();
}
std::fs::write(out_dir.as_ref().join("msvc/msvc.rc"), output).expect("couldn't write rc file");
std::fs::write(rc_dir.as_ref().join("msvc.rc"), output).expect("couldn't write rc file");
}
20 changes: 13 additions & 7 deletions main/build.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};

fn main() -> Result<(), Box<dyn Error>> {
// Generate "built" file (containing build-time information)
built::write_built_file().expect("Failed to acquire build-time information");

// Generate GUI dialog files (rc file and C header)
realearn_dialogs::generate_dialog_files("src/infrastructure/ui");
let bindings_file = "src/infrastructure/ui/bindings.rs";
let generated_dir = PathBuf::from("../target/generated");
let dialog_rc_file = generated_dir.join("msvc.rc");
fs::create_dir_all(&generated_dir)?;
realearn_dialogs::generate_dialog_files(&generated_dir, bindings_file);

// On macOS and Linux, try to generate SWELL dialogs (needs PHP)
#[cfg(target_family = "unix")]
if let Err(e) = generate_dialogs() {
if let Err(e) = generate_dialogs(&dialog_rc_file) {
println!("cargo:warning={}", e);
}

Expand All @@ -19,7 +25,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// Embed or compile dialogs
#[cfg(target_family = "windows")]
embed_dialog_resources();
embed_dialog_resources(&dialog_rc_file);
#[cfg(target_family = "unix")]
compile_dialogs();

Expand Down Expand Up @@ -104,14 +110,14 @@ fn compile_dialogs() {

/// On Windows we can directly embed the dialog resource file produced by ResEdit.
#[cfg(target_family = "windows")]
fn embed_dialog_resources() {
fn embed_dialog_resources(rc_file: impl AsRef<Path>) {
let target = std::env::var("TARGET").unwrap();
if let Some(tool) = cc::windows_registry::find_tool(target.as_str(), "cl.exe") {
for (key, value) in tool.env() {
std::env::set_var(key, value);
}
}
embed_resource::compile("src/infrastructure/ui/msvc/msvc.rc");
embed_resource::compile(rc_file);
}

#[cfg(feature = "generate")]
Expand Down Expand Up @@ -152,11 +158,11 @@ mod codegen {
/// Generates dialog window C++ code from resource file using SWELL's PHP-based dialog generator
/// (too obscure to be ported to Rust).
#[cfg(target_family = "unix")]
pub fn generate_dialogs() -> Result<(), Box<dyn Error>> {
pub fn generate_dialogs(rc_file: impl AsRef<Path>) -> Result<(), Box<dyn Error>> {
// Use PHP to translate SWELL-compatible RC file to C++
let result = std::process::Command::new("php")
.arg("lib/WDL/WDL/swell/swell_resgen.php")
.arg("src/infrastructure/ui/msvc/msvc.rc")
.arg(rc_file.as_ref())
.output()
.expect("PHP dialog translator result not available");
if !result.status.success() {
Expand Down
4 changes: 2 additions & 2 deletions main/src/infrastructure/ui/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define SWELL_PROVIDED_BY_APP

// Some preparation for dialog generation.
#include "msvc/resource.h"
#include "../../../../target/generated/resource.h"
#include "../../../lib/WDL/WDL/swell/swell.h"
// Make sure the following factors correspond to the ones in `units.rs` (function `effective_scale_factors`).
#ifdef __APPLE__
Expand All @@ -23,4 +23,4 @@
#define SS_WORDELLIPSIS 0

// This is the result of the dialog RC file conversion (via PHP script).
#include "msvc/msvc.rc_mac_dlg"
#include "../../../../target/generated/msvc.rc_mac_dlg"
Binary file removed main/src/infrastructure/ui/msvc/msvc.rc
Binary file not shown.
Loading

0 comments on commit 6a3c65d

Please sign in to comment.