Skip to content

Commit

Permalink
Don't download region.template from github
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen D committed Oct 21, 2024
1 parent b727e85 commit 5edda1b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 46 deletions.
36 changes: 2 additions & 34 deletions src/data_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ use crate::osm_parser::ProcessedElement;
use crate::world_editor::WorldEditor;
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::blocking::get;
use std::fs;
use std::io::Write;
use std::path::Path;

pub fn generate_world(
elements: Vec<ProcessedElement>,
Expand All @@ -17,22 +13,11 @@ pub fn generate_world(
) {
println!("{} {}", "[3/5]".bold(), "Processing data...");

let region_template_path: &str = "region.template";
let region_dir: String = format!("{}/region", args.path);
let ground_level: i32 = -62;

// Check if the region.template file exists, and download if necessary
if !Path::new(region_template_path).exists() {
let _ = download_region_template(region_template_path);
}

let mut editor: WorldEditor = WorldEditor::new(
region_template_path,
&region_dir,
scale_factor_x,
scale_factor_z,
&args,
);
let mut editor: WorldEditor =
WorldEditor::new(&region_dir, scale_factor_x, scale_factor_z, &args);

// Process data
let process_pb: ProgressBar = ProgressBar::new(elements.len() as u64);
Expand Down Expand Up @@ -207,20 +192,3 @@ pub fn generate_world(

println!("{}", "Done! World generation complete.".green().bold());
}

/// Downloads the region template file from a remote URL and saves it locally.
fn download_region_template(file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
let url = "https://github.com/louis-e/arnis/raw/refs/heads/main/region.template";

// Download the file
let response = get(url)?;
if !response.status().is_success() {
return Err(format!("Failed to download file: HTTP {}", response.status()).into());
}

// Write the file to the specified path
let mut file = fs::File::create(file_path)?;
file.write_all(&response.bytes()?)?;

Ok(())
}
File renamed without changes.
22 changes: 10 additions & 12 deletions src/world_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -111,7 +112,6 @@ impl WorldToModify {
}

pub struct WorldEditor<'a> {
region_template_path: String,
region_dir: String,
world: WorldToModify,
scale_factor_x: f64,
Expand All @@ -121,15 +121,8 @@ pub struct WorldEditor<'a> {

impl<'a> WorldEditor<'a> {
/// Initializes the WorldEditor with the region directory and template region path.
pub fn new(
region_template_path: &str,
region_dir: &str,
scale_factor_x: f64,
scale_factor_z: f64,
args: &'a Args,
) -> Self {
pub fn new(region_dir: &str, scale_factor_x: f64, scale_factor_z: f64, args: &'a Args) -> Self {
Self {
region_template_path: region_template_path.to_string(),
region_dir: region_dir.to_string(),
world: WorldToModify::default(),
scale_factor_x,
Expand All @@ -141,14 +134,19 @@ impl<'a> WorldEditor<'a> {
/// Creates a region for the given region coordinates.
fn create_region(&self, region_x: i32, region_z: i32) -> Region<File> {
let out_path: String = format!("{}/r.{}.{}.mca", self.region_dir, region_x, region_z);
std::fs::copy(&self.region_template_path, &out_path)
.expect("Failed to copy region template");
let region_file = File::options()

const REGION_TEMPLATE: &[u8] = include_bytes!("region.template");

let mut region_file = File::options()
.read(true)
.write(true)
.open(&out_path)
.expect("Failed to open region file");

region_file
.write_all(REGION_TEMPLATE)
.expect("Could not write region template");

Region::from_stream(region_file).expect("Failed to load region")
}

Expand Down

0 comments on commit 5edda1b

Please sign in to comment.