-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pl
129 lines (108 loc) · 3.18 KB
/
main.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# main.pl
#
# This file manages the program arguments, the initialization of the scene file,
# and loads the game in normal mode.
#
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use lib '.';
use creator::mainmenu;
use frame::base;
use frame::gameHandler;
use reader::import;
use reader::importchars;
use reader::readplot;
use reader::sceneselector;
use util::terminal;
# Displays the version text and quits
#
sub VersionText {
print "Bob 1.0.1\n";
exit;
}
# Displays the help text and exits
#
sub HelpText {
print "Usage: perl main.pl [OPTION]... [FILE]\n";
print "Framework for text-based games.";
print "\n";
print "With no FILE, prompts user to choose one from current directory.\n";
print "\n";
print " -c, --creator\t\tuse creator mode\n";
print " --help\t\tdisplay this help and exit\n";
print " --version\t\toutput version information and exit\n";
exit;
}
# Main Function
#
# Acts as a pseudo-entry point to the program
#
# arg1: Flattened array of command line arguments
#
sub main {
# A C-like main function as the entry point.
# argc and argv meaning "argument count" and "argument vector"
my $argc = scalar(@_);
my @argv = @_;
# forces flush after every print
$| = 1;
# read through all command-line arguments
my $filename = "";
my $creator = 0;
foreach my $arg (@argv) {
if ($arg eq "--version") {
VersionText();
} elsif ($arg eq "--help") {
HelpText();
} elsif ($arg eq "-c" || $arg eq "--creator") {
$creator = 1;
} elsif (substr($arg, 0, 1) eq '-') {
print "$arg: Invalid argument\n";
exit;
} else {
$filename = $arg;
}
}
# check if program is entering creator mode
if ($creator == 1) {
creator::MainMenu($filename);
util::PrintAtPos('l', 'b', "Press <ENTER> to continue...");
util::SetCursorPos('r', 'b');
<STDIN>;
system("clear");
exit;
}
# tests, tb removed
# my @dummy = ("Hello", "World!");
# frame::gameHandler::SaveGame(\@dummy);
# @dummy = frame::gameHandler::GetSaves();
# frame::gameHandler::LoadGame();
# exit;
# call SceneSelector module if user didn't supply a scene
my %data = reader::SceneSelector() if ($filename eq "");
if (!(exists $data{'scenefile'})) {
print "Selected file is not a valid scene file!\n";
exit;
}
$filename = $data{'scenefile'};
# make sure we could open the file, and give a friendly message if we can't
if (!(open(my $fh, '<:encoding(UTF-8)', $filename))) {
print "Cannot read from $filename: $!\n";
exit;
}
# inflate the scene file
my %section;
my @file = reader::Import($filename, \%section);
my %chars = reader::ImportChars(\@file, \%section);
if (exists $data{'savefile'}) {
print "Loaded game from $data{'savefile'}";
delete($data{'savefile'});
sleep(2);
} else {
frame::welcome();
}
frame::scene(\@file, \%section, \%chars, \%data);
}
# Program Entry Point
main(@ARGV);