Skip to content

Instantly share code, notes, and snippets.

@ConnorBP
Created September 18, 2024 13:27
Show Gist options
  • Save ConnorBP/cccccd433581fe43ad15d6f569553bcc to your computer and use it in GitHub Desktop.
Save ConnorBP/cccccd433581fe43ad15d6f569553bcc to your computer and use it in GitHub Desktop.
bevy free trial demo
// A bevy 0.14 plugin which sets a date of death for an alpha release of your game.
// helps fight against old alpha builds from being used when product goes comercial.
// Somewhat of a "free trial" system, or a "birthday bomb" causing the program to fail.
// Especially effective in networked scenarios where system time cannot be changed as easily.
//
// Plugin Usage:
// App::new()
// .insert_resource(ClearColor(Color::NONE))
// .add_plugins((
// DefaultPlugins,
// anti_dbg::AntiDbg
// )).run();
pub struct AntiDbg;
use bevy::{prelude::*, time::Stopwatch};
use chrono::{DateTime, Duration, Utc};
fn check_date_system(
mut sw: Local<Stopwatch>,
time: Res<Time>,
) {
// tick the time each frame by frame delta
sw.tick(time.delta());
// only check if birthday has arrived every 5 seconds
if sw.elapsed_secs() > 5. {
// make this program dead in the water after 30 days
let deadline = Utc::now() - Duration::days(30);
// let deadline = Utc::now() - Duration::minutes(2); // for testing that it works
let build_date = DateTime::<Utc>::from_timestamp(std::env!("DATE").parse().unwrap(),0).unwrap();
// debug output our UTC times for demonstration purposes. (Remove this for serious usages)
println!("Got build date: {} and 30 days ago {}", build_date, deadline);
if deadline > build_date {
std::process::exit(0);
// backup panic in case exit is skipped somehow
panic!("");
// 2nd backup anti-debug catcher
unsafe {
asm!(
"xor eax, eax",
"push offset l2",
"push d fs:[eax]",
"mov fs:[eax], esp" ,
"push fs",
"pop ss",
"xchg [eax], cl",
"xchg [eax], cl",
"int 3", // force breakpoint
"int 29h" // fast fail on windows
);
}
}
// reset the stopwatch timer when we have reached 5 seconds
sw.reset();
}
}
// this allows this system to simply be added as a plugin on bevy init
impl Plugin for AntiDbg {
fn build(&self, app: &mut App) {
app.add_systems(PreUpdate, (check_date_system));
}
}
use std::process::Command;
// chrono package must be in build-dependencies
use chrono::Utc;
fn main() {
println!("cargo:rerun-if-changed=.git/HEAD");
// note: add error checking yourself.
let output = Command::new("git")
.args(&["rev-parse", "HEAD"])
.output()
.unwrap();
let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
let date = Utc::now();
println!("cargo:rustc-env=DATE={}", date.timestamp());
//println!("cargo:rerun-if-env-changed=DATE");
println!("cargo:rerun-if-changed=./*");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment